Source code for bag.subcommand

"""Decorator that makes it easy to use **argh** to create subcommands.


    from bag.subcommand import subcommand, main

    def my_subcommand(foo:'Help for foo'='default value'):

    if __name__ == '__main__':

[docs]def subcommand(fn): """Decorate ``fn`` adding it to a list.""" if not hasattr(subcommand, 's'): # Because functions are 1st class objects subcommand.s = [] # we can store the list inside the function. subcommand.s.append(fn) # One global variable less. return fn
[docs]def main(): """Use argh to dispatch to your subcommands.""" from argh import ArghParser # sudo apt-get install python3-argh parser = ArghParser() # Sorting makes the output of --help better: parser.add_commands(sorted(subcommand.s, key=lambda f: f.__name__)) # parser.add_commands(subcommand.s) parser.dispatch()