Stringing Statements Together
❘
47
In this case, the application calls
main()
, the new main function. If you’ve spent any time
writing C or C++ code, this version of
main()
will look a little familiar. While it lacks
argc
(the argument count), this
main()
does have an
argv
. However, the arguments can change
between the time the code starts the application and begins processing
main()
, so you want
to set
argv
to
None
(essentially, nothing at all in IronPython), and obtain the command line
arguments from
sys.argv
.
At this point, the code uses a
for
loop to parse
argv
and displays its arguments on screen.
At a minimum, you get the name of the application. The arguments you see depend on their
order on the command line (or in Visual Studio). For example, if you specify
-D
after the file-
name, then you see it as part of the arguments. Figure 3-1 shows an example of what a series
of arguments might look like when using a command line of
NewMain.py -D -c These are
arguments
. The code ends by pausing so you can see the output when using the Visual Studio
debugger.
FIgURE 3-1:
Create a main() function and then process the command line arguments in it.
Notice the levels of indentation in this example. The levels of indentation show the structure of the
application. Unlike many application development languages, IronPython enforces indentation, with
the result that you can see the application structure quite easily.
In IronPython, structure comes in a number of forms, all of which are accessible to you as the
developer. Using structure properly makes your applications easier to understand. From the main
(and any other) function, you can use the following structural elements:
➤
Import external files
➤
Call other functions
➤
➤
Use decision-making or loop structures
➤
➤
Interact with objects
➤
➤
Chapter 5 demonstrates IronPython objects in detail. For now, you need to consider that IronPython
uses many objects that don’t appear as objects at first. For example, later in this chapter you’ll discover
that the common string actually provides a number of methods you can use to modify its content. You’ll
encounter more objects as the book progresses. For now, focus on the basic structure that IronPython
provides so you don’t get lost when creating your own applications.
548592c03.indd 47
2/24/10 12:47:29 PM
www.finebook.ir
Do'stlaringiz bilan baham: |