Python argparse - Mutually exclusive group with default if no argument is given -


i'm writing python script process machine-readable file , output human-readable report on data contained within.
give option of outputting data stdout (-s) (by default) or txt (-t) or csv (-c) file. have switch default behaviour, many commands do.

in terms of usage:, i'd see script [-s | -c | -t] input file, , have -s default if no arguments passed.

i have (for relevant args, in brief):

parser = argparse.argumentparser() group = parser.add_mutually_exclusive_group() group.add_argument('-s', '--stdout', action='store_true') group.add_argument('-c', '--csv', action='store_true') group.add_argument('-t', '--txt', action='store_true') args = parser.parse_args()  if not any((args.stdout, args.csv, args.txt)):     args.stdout = true 

so if none of -s, -t, or -c set, stdout (-s) forced true, if -s had been passed.

is there better way achieve this? or approach entirely considered 'better' reason?

note: i'm using python 3.5.1/2 , i'm not worried compatibility other versions, there no plan share script others @ point. it's make life easier.

you have each of actions update same variable, supplying stdout default value variable.

consider program:

import argparse  parser = argparse.argumentparser() group = parser.add_mutually_exclusive_group() group.add_argument(     '-s', '--stdout', action='store_const', dest='type', const='s', default='s') group.add_argument(     '-c', '--csv', action='store_const', dest='type', const='c') group.add_argument(     '-t', '--txt', action='store_const', dest='type', const='t') args = parser.parse_args() print args 

your code like:

if args.type == 's':     ofile = sys.stdout elif args.type == 'c':     ofile = ... ... 

first alternative:

rather arbitrarily choose 1 of .add_argument()s specify default type, can use parser.set_defaults() specify default type.

import argparse  parser = argparse.argumentparser() group = parser.add_mutually_exclusive_group() group.add_argument('-s', '--stdout', action='store_const', dest='type', const='s') group.add_argument('-c', '--csv', action='store_const', dest='type', const='c') group.add_argument('-t', '--txt', action='store_const', dest='type', const='t') parser.set_defaults(type='s') args = parser.parse_args() print args 

second alternative:

rather specify type enumerated value, store callable type, , invoke callable:

import argparse  def do_stdout():     # required support stdout     print("stdout!")     return def do_csv():     # required support csv file     print("csv!")     return def do_text():     # required support txt file     print("text!")     return  parser = argparse.argumentparser() group = parser.add_mutually_exclusive_group() group.add_argument('-s', '--stdout', action='store_const', dest='type', const=do_stdout) group.add_argument('-c', '--csv', action='store_const', dest='type', const=do_csv) group.add_argument('-t', '--txt', action='store_const', dest='type', const=do_text) parser.set_defaults(type=do_stdout) args = parser.parse_args() print args args.type() 

Comments