Chris Redford
Chris Redford

Reputation: 17778

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

Upvotes: 5

Views: 9343

Answers (1)

SimplyKnownAsG
SimplyKnownAsG

Reputation: 934

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()

Upvotes: 11

Related Questions