Package: captain
CaptainEasy python cli scripts for people that just want get things done.Important- If you have older scripts you might need thecaptain~=3.0.0 branch. The mainline branch has an entirely different interface.UsageA validcaptaincli script needs just two things:ADefaultclass that extendscaptain.Commandand has ahandle()method:fromcaptainimportCommandclassDefault(Command):defhandle(self,foo,bar):return0Callingcaptain.handle()at the end of your script:fromcaptainimportCommand,handleclassDefault(Command):defhandle(self,foo,bar):return0if__name__=="__main__":handle()That's it! Whatever arguments you define in your class'sDefault.handle()method will be options on the command line. A captain script is called just like any other python command line script, so to run the above example you could do:$ python path/to/script.py --foo=1 --bar=2Argument DecoratorThecaptain.arg()decorator provides a nice passthrough api to the fullargparse.ArgumentParser.add_argument() methodif you want to fine tune how arguments are passed into your script:fromcaptainimportCommand,handle,argclassDefault(Command):@arg('--foo','-f',action="store_true")@arg('arg',metavar='ARG')defhandle(self,*args,**kwargs):'''this is the help description'''self.output.out(args)self.output.out(kwargs)if__name__=="__main__":handle()Would print a help string like this:usage: script.py [-h] [--foo FOO] ARG

this is the help description

positional arguments:
  ARG

optional arguments:
  -h, --help         show this help message and exit
  --foo FOO, -f FOOCommand OutputThecaptain.io.Outputclass makes it easy to print stuff in your script while still giving you full control by being able to configure the logger if you need to. It also will obey the global--quietflag that Captain adds to every script.It's available in thehandle()method by usingself.output:fromcaptainimportCommandclassDefault(Command):defhandle(self,*args,**kwargs):var1="print"var2="stdout"self.output.out("this will{}to{}",var1,var2)var2="stderr"self.output.err("this will{}to{}",var1,var2)e=ValueError("this will print with stacktrace and everything")self.output.exception(e)Thecaptain.io.Outputclass has a lot of nice little helper methods but Captain can also work with modules likeclintif you need to do more advanced cli output.ExamplesA typical standard python cli scriptimportargparseif__name__=='__main__':parser=argparse.ArgumentParser(description='fancy script description')parser.add_argument("--foo",action='store_true')parser.add_argument("--bar",default=0,type=int)parser.add_argument("args",nargs='*')args=parser.parse_args()sys.exit(0)would become:importcaptainclassDefault(captain.Command):defhandle(foo=False,bar=0,*args):'''fancy script description'''return0if__name__=='__main__':captain.handle()SubcommandsCaptain supports multiple subcommands defined in the script by naming yourcaptain.Commandchild classes something other thanDefault:# cli.pyimportcaptainclassFoo(captain.Command):defhandle(self):passclassBar(captain.Command):defhandle(self):passif__name__=='__main__':captain.handle()Sofoocould be called using:$ python cli.py fooAndbarcould be called using:$ python cli.py barEmbedding captain in another packageIf you want a script from you package to be usable using bothpython -m exampleand maybe aconsole_scriptsentry point defined insetup.py, you can set up your package's__main__.pymodule like this:# example/__main__.pyfromcaptainimportCommand,handleclassDefault(captain.Command):defhandle(self):passif__name__=="__main__":handle()And then in yoursetup.pyscript you can add:entry_points={'console_scripts':['example = example.__main__:handle'],}That's all there is to it.InstallUse pip:$ pip install captainFor latest and greatest:$ pip install -U "git+https://github.com/Jaymon/captain#egg=captain"