Skip to content

Instantly share code, notes, and snippets.

@frenchtoast747
Created September 11, 2017 18:51
Show Gist options
  • Save frenchtoast747/cdbdba055b649f44d0f86bc88d29b6b8 to your computer and use it in GitHub Desktop.
Save frenchtoast747/cdbdba055b649f44d0f86bc88d29b6b8 to your computer and use it in GitHub Desktop.
Flag per function - Argparse
import argparse
class c1(object):
def cleanup(self):
print 'c1'
class c2(object):
def cleanup(self):
print 'c2'
class c3(object):
def cleanup(self):
print 'c3'
parser = argparse.ArgumentParser()
parser.add_argument('-1', const=c1, dest='cleaners', action='append_const')
parser.add_argument('-2', const=c2, dest='cleaners', action='append_const')
parser.add_argument('-3', const=c3, dest='cleaners', action='append_const')
args = parser.parse_args()
if args.cleaners is not None:
for cleaner_class in args.cleaners:
cleaner = cleaner_class()
cleaner.cleanup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment