Professional IronPython


Understanding the Command Line



Download 20,35 Mb.
Pdf ko'rish
bet214/453
Sana01.07.2022
Hajmi20,35 Mb.
#723045
1   ...   210   211   212   213   214   215   216   217   ...   453
Understanding the Command Line
 

 
197
Of course, you’ll want to expand beyond simply listing the command line arguments into doing some-
thing with them. Listing 10-2 shows an example of how you could parse command line arguments for 
the typical Windows user.
LISTINg 10-2: 
 Using the Python approach to parse command line arguments
# Perform the required imports.
import sys 
import getopt
# Obtain the command line arguments.
def main(argv):
try:
# Obtain the options and arguments.
opts, args = getopt.getopt(argv, ‘Dh?g:s’, [‘help’, ‘Greet=’, ‘Hello’])
# Parse the command line options.
for opt, arg in opts:
# Display help when requested.
if opt in (‘-h’, ‘-?’, ‘--help’):
usage()
sys.exit()
# Tell the user we’re in debug mode.
if opt in (‘-D’):
print ‘Application in Debug mode.’
# Display a user greeting.
if opt in (‘-g’, ‘--Greet’):
print ‘Good to see you’, arg.strip(‘:’)
# Say hello to the user.
if opt in (‘-s’, ‘--Hello’):
print ‘Hello!’
# Parse the command line arguments.
for arg in args:
# Display help when requested.
if arg.upper() in (‘/?’, ‘/HELP’):
usage()
sys.exit()
# Tell the user we’re in Debug mode.
elif arg in (‘/D’):
print ‘Application in Debug mode.’
# Display a user greeting.
elif ‘/GREET’ in arg.upper() or ‘/G’ in arg.upper():
print ‘Good to see you’, arg.split(‘:’)[1]
# Say hello to the user.
continues
548592c10.indd 197
2/24/10 12:48:28 PM
www.finebook.ir


198

CHAPTER 10
Using ironPython for AdministrAtion tAsks
elif arg.upper() in (‘/S’, ‘/HELLO’):
print ‘Hello!’
# User has provided bad input.
else:
raise getopt.GetoptError(‘Error in input.’, arg)
# The user supplied command line contains illegal arguments.
except getopt.GetoptError:
# Display the usage information.
usage()
# exit with an error code.
sys.exit(2)
# Call main() with only the relevant arguments.
if __name__ == “__main__“:
main(sys.argv[1:])
# Pause after the debug session.
raw_input(‘\nPress any key to continue...’)
This example actually begins at the bottom of the listing with an 
if
statement:
if __name__ == “__main__“:
main(sys.argv[1:])
Many of your IronPython applications will use this technique to pass just the command line arguments 
to the 
main()
function. As shown in Figure 10-1, the first command line argument is the name of the 
script and you don’t want to attempt processing it.
Python assumes that everyone works with Linux or some other form of Unix. Consequently, it only 
supports the short dash (–) directly for command line options. An option is an input that you can 
parse without too much trouble because Python does most of the work for you. Options use a single 
dash for a single letter (short option) or a double dash for phrases (long option). Anything that doesn’t 
begin with a dash, such as something that begins with a slash (/) is an argument. Unfortunately, most 
of your Windows users will be familiar with arguments, not options, so your application should pro-
cess both.
The code begins by separating options and arguments that you’ve defined. The 
getopt.getopt()
method requires three arguments:

The list of options and arguments to process

A list of short options


A list of long options


In this example, 
argv
contains the list of options and arguments contained in the command line, 
except for the script name. Each option and argument is separated by a space in the original string.

Download 20,35 Mb.

Do'stlaringiz bilan baham:
1   ...   210   211   212   213   214   215   216   217   ...   453




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish