Skip to main content
fixed typesetting error where asterisks made italics by mistake
Source Link

Using nargs parameter in argparse's add_argument method

I use nargs='' as an add_argument parameter. I specifically used nargs=''nargs='*' as an add_argument parameter. I specifically used nargs='*' to the option to pick defaults if I am not passing any explicit arguments

Including a code snippet as example:

Example: temp_args1.py

Please Note: The below sample code is written in python3. By changing the print statement format, can run in python2

#!/usr/local/bin/python3.6 from argparse import ArgumentParser description = 'testing for passing multiple arguments and to get list of args' parser = ArgumentParser(description=description) parser.add_argument('-i', '--item', action='store', dest='alist', type=str, nargs='*', default=['item1', 'item2', 'item3'], help="Examples: -i item1 item2, -i item3") opts = parser.parse_args() print("List of items: {}".format(opts.alist)) 

Note: I am collecting multiple string arguments that gets stored in the list - opts.alistopts.alist If you want list of integers, change the type parameter on parser.add_argumentparser.add_argument to intint

Execution Result:

python3.6 temp_agrs1.py -i item5 item6 item7 List of items: ['item5', 'item6', 'item7'] python3.6 temp_agrs1.py -i item10 List of items: ['item10'] python3.6 temp_agrs1.py List of items: ['item1', 'item2', 'item3'] 

Using nargs parameter in argparse's add_argument method

I use nargs='' as an add_argument parameter. I specifically used nargs='' to the option to pick defaults if I am not passing any explicit arguments

Including a code snippet as example:

Example: temp_args1.py

Please Note: The below sample code is written in python3. By changing the print statement format, can run in python2

#!/usr/local/bin/python3.6 from argparse import ArgumentParser description = 'testing for passing multiple arguments and to get list of args' parser = ArgumentParser(description=description) parser.add_argument('-i', '--item', action='store', dest='alist', type=str, nargs='*', default=['item1', 'item2', 'item3'], help="Examples: -i item1 item2, -i item3") opts = parser.parse_args() print("List of items: {}".format(opts.alist)) 

Note: I am collecting multiple string arguments that gets stored in the list - opts.alist If you want list of integers, change the type parameter on parser.add_argument to int

Execution Result:

python3.6 temp_agrs1.py -i item5 item6 item7 List of items: ['item5', 'item6', 'item7'] python3.6 temp_agrs1.py -i item10 List of items: ['item10'] python3.6 temp_agrs1.py List of items: ['item1', 'item2', 'item3'] 

Using nargs parameter in argparse's add_argument method

I use nargs='*' as an add_argument parameter. I specifically used nargs='*' to the option to pick defaults if I am not passing any explicit arguments

Including a code snippet as example:

Example: temp_args1.py

Please Note: The below sample code is written in python3. By changing the print statement format, can run in python2

#!/usr/local/bin/python3.6 from argparse import ArgumentParser description = 'testing for passing multiple arguments and to get list of args' parser = ArgumentParser(description=description) parser.add_argument('-i', '--item', action='store', dest='alist', type=str, nargs='*', default=['item1', 'item2', 'item3'], help="Examples: -i item1 item2, -i item3") opts = parser.parse_args() print("List of items: {}".format(opts.alist)) 

Note: I am collecting multiple string arguments that gets stored in the list - opts.alist If you want list of integers, change the type parameter on parser.add_argument to int

Execution Result:

python3.6 temp_agrs1.py -i item5 item6 item7 List of items: ['item5', 'item6', 'item7'] python3.6 temp_agrs1.py -i item10 List of items: ['item10'] python3.6 temp_agrs1.py List of items: ['item1', 'item2', 'item3'] 
fix formatting
Source Link
wjandrea
  • 34k
  • 10
  • 69
  • 105

Using nargs parameter in argparse's add_argument method

I use nargs='' as an add_argument parameter. I specifically used nargs='' to the option to pick defaults if I am not passing any explicit arguments

Including a code snippet as example:

Example: temp_args1.py

Please Note: The below sample code is written in python3. By changing the print statement format, can run in python2

 #!/usr/local/bin/python3.6  from argparse import ArgumentParser  description = 'testing for passing multiple arguments and to get list of args'  parser = ArgumentParser(description=description)  parser.add_argument('-i', '--item', action='store', dest='alist',   type=str, nargs='*', default=['item1', 'item2', 'item3'],   help="Examples: -i item1 item2, -i item3")  opts = parser.parse_args()  print("List of items: {}".format(opts.alist)) 

Note: I am collecting multiple string arguments that gets stored in the list - opts.alist If you want list of integers, change the type parameter on parser.add_argument to int

Execution Result:

 python3.6 temp_agrs1.py -i item5 item6 item7  List of items: ['item5', 'item6', 'item7']  python3.6 temp_agrs1.py -i item10  List of items: ['item10']  python3.6 temp_agrs1.py  List of items: ['item1', 'item2', 'item3'] 

Using nargs parameter in argparse's add_argument method

I use nargs='' as an add_argument parameter. I specifically used nargs='' to the option to pick defaults if I am not passing any explicit arguments

Including a code snippet as example:

Example: temp_args1.py

Please Note: The below sample code is written in python3. By changing the print statement format, can run in python2

 #!/usr/local/bin/python3.6  from argparse import ArgumentParser  description = 'testing for passing multiple arguments and to get list of args'  parser = ArgumentParser(description=description)  parser.add_argument('-i', '--item', action='store', dest='alist',   type=str, nargs='*', default=['item1', 'item2', 'item3'],   help="Examples: -i item1 item2, -i item3")  opts = parser.parse_args()  print("List of items: {}".format(opts.alist)) 

Note: I am collecting multiple string arguments that gets stored in the list - opts.alist If you want list of integers, change the type parameter on parser.add_argument to int

Execution Result:

 python3.6 temp_agrs1.py -i item5 item6 item7  List of items: ['item5', 'item6', 'item7']  python3.6 temp_agrs1.py -i item10  List of items: ['item10']  python3.6 temp_agrs1.py  List of items: ['item1', 'item2', 'item3'] 

Using nargs parameter in argparse's add_argument method

I use nargs='' as an add_argument parameter. I specifically used nargs='' to the option to pick defaults if I am not passing any explicit arguments

Including a code snippet as example:

Example: temp_args1.py

Please Note: The below sample code is written in python3. By changing the print statement format, can run in python2

#!/usr/local/bin/python3.6 from argparse import ArgumentParser description = 'testing for passing multiple arguments and to get list of args' parser = ArgumentParser(description=description) parser.add_argument('-i', '--item', action='store', dest='alist', type=str, nargs='*', default=['item1', 'item2', 'item3'], help="Examples: -i item1 item2, -i item3") opts = parser.parse_args() print("List of items: {}".format(opts.alist)) 

Note: I am collecting multiple string arguments that gets stored in the list - opts.alist If you want list of integers, change the type parameter on parser.add_argument to int

Execution Result:

python3.6 temp_agrs1.py -i item5 item6 item7 List of items: ['item5', 'item6', 'item7'] python3.6 temp_agrs1.py -i item10 List of items: ['item10'] python3.6 temp_agrs1.py List of items: ['item1', 'item2', 'item3'] 
added 492 characters in body
Source Link
Py_minion
  • 2.1k
  • 1
  • 18
  • 20

Using nargs parameter in argparse's add_argument method

I use nargs='*' as an add_argument parameter.nargs='' as an add_argument parameter. I specifically used nargs='' to the option to pick defaults if I am not passing any explicit arguments

Including a code snippet as example:

Example: temp_args1.py

Please Note: The below sample code is written in python3. By changing the print statement format, can run in python2

 #!/usr/local/bin/python3.6 from argparse import ArgumentParser description = 'testing for passing multiple arguments and to get list of args' parser = ArgumentParser(description=description) parser.add_argument('-i', '--item', action='store', dest='alist', type=str, nargs='*', default=['item1', 'item2', 'item3'], help="Examples: -i item1 item2, -i item3") opts = parser.parse_args() print("List of items: {}".format(opts.alist)) 

Note: I am collecting multiple string arguments that gets stored in the list - opts.alist If you want list of integers, change the type parameter on parser.add_argument to int

Execution Result:

 python3.6 temp_agrs1.py -i item5 item6 item7 List of items: ['item5', 'item6', 'item7'] python3.6 temp_agrs1.py -i item10 List of items: ['item10'] python3.6 temp_agrs1.py List of items: ['item1', 'item2', 'item3'] 

I use nargs='*' as an add_argument parameter.

Including a code snippet as example:

 #!/usr/local/bin/python3.6 from argparse import ArgumentParser description = 'testing for passing multiple arguments and to get list of args' parser = ArgumentParser(description=description) parser.add_argument('-i', '--item', action='store', dest='alist', type=str, nargs='*', default=['item1', 'item2', 'item3'], help="Examples: -i item1 item2, -i item3") opts = parser.parse_args() print("List of items: {}".format(opts.alist)) 

Note: I am collecting multiple string arguments that gets stored in the list - opts.alist If you want list of integers, change the type parameter on parser.add_argument to int

Using nargs parameter in argparse's add_argument method

I use nargs='' as an add_argument parameter. I specifically used nargs='' to the option to pick defaults if I am not passing any explicit arguments

Including a code snippet as example:

Example: temp_args1.py

Please Note: The below sample code is written in python3. By changing the print statement format, can run in python2

 #!/usr/local/bin/python3.6 from argparse import ArgumentParser description = 'testing for passing multiple arguments and to get list of args' parser = ArgumentParser(description=description) parser.add_argument('-i', '--item', action='store', dest='alist', type=str, nargs='*', default=['item1', 'item2', 'item3'], help="Examples: -i item1 item2, -i item3") opts = parser.parse_args() print("List of items: {}".format(opts.alist)) 

Note: I am collecting multiple string arguments that gets stored in the list - opts.alist If you want list of integers, change the type parameter on parser.add_argument to int

Execution Result:

 python3.6 temp_agrs1.py -i item5 item6 item7 List of items: ['item5', 'item6', 'item7'] python3.6 temp_agrs1.py -i item10 List of items: ['item10'] python3.6 temp_agrs1.py List of items: ['item1', 'item2', 'item3'] 
Source Link
Py_minion
  • 2.1k
  • 1
  • 18
  • 20
Loading