How to read/process command line arguments?
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Underwater World
--
Chapters
00:00 Question
00:44 Accepted answer (Score 581)
01:22 Answer 2 (Score 662)
01:43 Answer 3 (Score 131)
03:32 Answer 4 (Score 76)
04:09 Thank you
--
Full question
https://stackoverflow.com/questions/1009...
Question links:
[What’s the best way to grab/parse command line arguments passed to a Python script?]: https://stackoverflow.com/questions/2006...
[Implementing a “[command] [action] [parameter]” style command-line interfaces?]: https://stackoverflow.com/questions/3624...
[How can I process command line arguments in Python?]: https://stackoverflow.com/questions/5678...
[How do I format positional argument help using Python’s optparse?]: https://stackoverflow.com/questions/6426...
Accepted answer links:
[docs]: https://docs.python.org/3/library/argpar...
Answer 2 links:
[sys.argv]: https://docs.python.org/3/library/sys.ht...
Answer 3 links:
[argparse]: http://code.google.com/p/argparse/
[these]: http://argparse.googlecode.com/svn/trunk...
Answer 4 links:
[argparse]: https://docs.python.org/library/argparse...
[the introduction to argparse]: https://docs.python.org/howto/argparse.h...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #commandline #commandlinearguments
#avk47
ANSWER 1
Score 700
import sys
print("\n".join(sys.argv))
sys.argv is a list that contains all the arguments passed to the script on the command line. sys.argv[0] is the script name.
Basically,
import sys
print(sys.argv[1:])
ACCEPTED ANSWER
Score 620
The canonical solution in the standard library is argparse (docs):
Here is an example:
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument("-f", "--file", dest="filename",
help="write report to FILE", metavar="FILE")
parser.add_argument("-q", "--quiet",
action="store_false", dest="verbose", default=True,
help="don't print status messages to stdout")
args = parser.parse_args()
argparse supports (among other things):
- Multiple options in any order.
- Short and long options.
- Default values.
- Generation of a usage help message.
ANSWER 3
Score 134
Just going around evangelizing for argparse which is better for these reasons.. essentially:
(copied from the link)
argparse module can handle positional and optional arguments, while optparse can handle only optional arguments
argparse isn’t dogmatic about what your command line interface should look like - options like -file or /file are supported, as are required options. Optparse refuses to support these features, preferring purity over practicality
argparse produces more informative usage messages, including command-line usage determined from your arguments, and help messages for both positional and optional arguments. The optparse module requires you to write your own usage string, and has no way to display help for positional arguments.
argparse supports action that consume a variable number of command-line args, while optparse requires that the exact number of arguments (e.g. 1, 2, or 3) be known in advance
argparse supports parsers that dispatch to sub-commands, while optparse requires setting
allow_interspersed_argsand doing the parser dispatch manually
And my personal favorite:
- argparse allows the type and
action parameters to
add_argument()to be specified with simple callables, while optparse requires hacking class attributes likeSTORE_ACTIONSorCHECK_METHODSto get proper argument checking
ANSWER 4
Score 79
There is also argparse stdlib module (an "impovement" on stdlib's optparse module). Example from the introduction to argparse:
# script.py
import argparse
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'integers', metavar='int', type=int, choices=range(10),
nargs='+', help='an integer in the range 0..9')
parser.add_argument(
'--sum', dest='accumulate', action='store_const', const=sum,
default=max, help='sum the integers (default: find the max)')
args = parser.parse_args()
print(args.accumulate(args.integers))
Usage:
$ script.py 1 2 3 4
4
$ script.py --sum 1 2 3 4
10