fork(1) download
  1. #!/usr/bin/python
  2.  
  3. import argparse
  4.  
  5. parser=argparse.ArgumentParser(description='script to run')
  6.  
  7. parser.add_argument('inputFile', nargs='?', type=argparse.FileType('rt'))
  8. parser.add_argument('inputString', action='store', nargs='?')
  9. parser.add_argument('-option1', metavar='percent', type=float, action='store')
  10. parser.add_argument('-option2', metavar='outFile1', type=argparse.FileType('w'))
  11. parser.add_argument('-option3', action='store', default='<10')
  12. args = parser.parse_args()
  13. print(args)
  14.  
  15. for argv in [[], ['-h'], '/etc/passwd b'.split(), ['/etc/passwd']]:
  16. print("*"*79)
  17. try:
  18. print(parser.parse_args(argv))
  19. except SystemExit:
  20. pass
Success #stdin #stdout 0.17s 12072KB
stdin
Standard input is empty
stdout
Namespace(inputFile=None, inputString=None, option1=None, option2=None, option3='<10')
*******************************************************************************
Namespace(inputFile=None, inputString=None, option1=None, option2=None, option3='<10')
*******************************************************************************
usage: prog.py [-h] [-option1 percent] [-option2 outFile1] [-option3 OPTION3]
               [inputFile] [inputString]

script to run

positional arguments:
  inputFile
  inputString

optional arguments:
  -h, --help         show this help message and exit
  -option1 percent
  -option2 outFile1
  -option3 OPTION3
*******************************************************************************
Namespace(inputFile=<open file '/etc/passwd', mode 'rt' at 0xb72f57b0>, inputString='b', option1=None, option2=None, option3='<10')
*******************************************************************************
Namespace(inputFile=<open file '/etc/passwd', mode 'rt' at 0xb72f57b0>, inputString=None, option1=None, option2=None, option3='<10')