python - Parsing cmd args like typical filter programs -


i spent few hours reading tutorials argparse , managed learn use normal parameters. official documentation not readable me. i'm new python. i'm trying write program invoked in following ways:

cat infile | program [options] > outfile -- if no infile or outfile specified, read stdin , output stdout.

program [options] infile outfile

program [options] infile > outfile -- if 1 file specified input , output should go stdout.

cat infile | program [options] - outfile -- if '-' given in place of inflie read stdin.

program [options] /path/to/folder outfile -- process files /path/to/folder , subdirectories.

i want behave regular cli program under gnu/linux.

it nice if program able invoked:

program [options] infile0 infile1 ... infilen outfile -- first path/file interpreted input, last 1 interpreted output. additional ones interpreted inputs.

i write dirty code accomplish going used, end maintaining (and know live...).

any help/suggestions appreciated.


combining answers , more knowledge internet i've managed write this(it not accept multiple inputs enough):

import sys, argparse, os.path, glob  def inputfile(path):     if path == "-":         return [sys.stdin]     elif os.path.exists(path):         if os.path.isfile(path):             return [path]         else:             return [y x in os.walk(path) y in glob.glob(os.path.join(x[0], '*.dat'))]     else:         exit(2)  def main(argv):     cmdargsparser = argparse.argumentparser()     cmdargsparser.add_argument('infile', nargs='?', default='-', type=inputfile)     cmdargsparser.add_argument('outfile', nargs='?', default='-', type=argparse.filetype('w'))     cmdargs = cmdargsparser.parse_args()      print cmdargs.infile     print cmdargs.outfile  if __name__ == "__main__":    main(sys.argv[1:]) 

thank you!

you need positional argument (name not starting dash), optional arguments (nargs='?'), default argument (default='-'). additionally, argparse.filetype convenience factory return sys.stdin or sys.stdout if - passed (depending on mode).

all together:

#!/usr/bin/env python  import argparse  # default argument sys.argv[0] parser = argparse.argumentparser('foo') parser.add_argument('in_file', nargs='?', default='-', type=argparse.filetype('r')) parser.add_argument('out_file', nargs='?', default='-', type=argparse.filetype('w'))  def main():     # default argument is sys.argv[1:]     args = parser.parse_args(['bar', 'baz'])     print(args)     args = parser.parse_args(['bar', '-'])     print(args)     args = parser.parse_args(['bar'])     print(args)     args = parser.parse_args(['-', 'baz'])     print(args)     args = parser.parse_args(['-', '-'])     print(args)     args = parser.parse_args(['-'])     print(args)     args = parser.parse_args([])     print(args)  if __name__ == '__main__':     main() 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -