Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

argparse with multiple optional flags in one dash

Is it possible to associate multiple flags with a single dash in argparse as in this standard Linux argument style?

 tar -xvf some_filename.tar
like image 863
Chris Redford Avatar asked Jul 23 '14 19:07

Chris Redford


1 Answers

This will do the trick. Most likely, you didn't include the short form for each argument.

import argparse

parser = argparse.ArgumentParser(description='... saves many files together...')
parser.add_argument('--extract', '-x',
                    action='store_true',
                    help='extract files from an archive')
parser.add_argument('--verbose', '-v',
                    action='store_true',
                    help='verbosely list files processed')
parser.add_argument('--file', '-f',
                    # dest='file', -- only needed if the long form isn't first
                    help='use archive file or device ARCHIVE')

args = parser.parse_args()
like image 157
SimplyKnownAsG Avatar answered Nov 09 '22 07:11

SimplyKnownAsG