Command line interface
This document describes control flow used by the ricecooker framework, and the
particular logic used to parse command line arguments. Under normal use cases you
shouldn’t need modify the default command line parsing, but you need to understand
how things work in case want to customize the command line args for your chef script.
Summary
A sushi chef script like this:
#!/usr/bin/env python
...
...
class MySushiChef(SushiChef):
def get_channel(**kwargs)` -> ChannelNode (bare channel, used just for metadata)
...
def construct_channel(**kwargs) -> ChannelNode (with populated topic tree)
...
...
...
if __name__ == '__main__':
chef = MySushiChef()
chef.main()
Flow diagram
The call to chef.main() results in the following sequence of six calls:
MySushiChef -----extends----> SushiChef commands.uploadchannel
--------------------------- ----------------------- -----------------------
1. main()
2. parse_args_and_options()
3. run(args, options)
4. uploadchannel(chef, *args, **options)
...
5. get_channel(**kwargs)
...
6. construct_channel(**kwargs)
...
...
DONE
The chef script must define a subclass of
ricecooker.chefs.SushiChefthat implement the methodsget_channelandconstruct_channel:class MySushiChef(ricecooker.chefs.SushiChef): def get_channel(**kwargs)` -> ChannelNode (bare channel, used just for info) ... def construct_channel(**kwargs): --> ChannelNode (with populated Tree) ...Each chef script is a standalone python executable. The
mainmethod of the chef instance is the entry point used by a chef script:#!/usr/bin/env python ... ... ... if __name__ == '__main__': chef = MySushiChef() chef.main()
The
__init__method of the sushi chef class configures anargparseparser. The base classSushiChefcreatesself.arg_parserand a chef subclass can adds to this shared parser its own command line arguments. This is used only in vary special cases; for most cases, using the CLIoptionsis sufficient.The
mainmethod of theSushiChefclass parses the command line arguments and calls therunmethod:class SushiChef(): ... def main(self): args, options = self.parse_args_and_options() self.run(args, options)
The chef’s
runmethod callsuploadchannelclass SushiChef(): ... def run(self, args, options): ... uploadchannel(self, **args.__dict__, **options)
note the chef instance is passed as the first argument, and not path.
The
uploadchannelfunction expects the sushi chef class to implement the following two methods:get_channel(**kwargs): returns a ChannelNode (previously calledcreate_channel)as an alternative, if
MySushiChefhas achannel_infoattribute (a dict) then the default SushiChef.get_channel will create the channel from this info
construct_channel(**kwargs): create the channel and build node tree
Additionally, the
MySushiChefclass can implement the following optional methods that will be called as part of the run__init__: if you want to add custom chef-specific command line arguments using argparsepre_run: if you need to do something before chef run starts (called byrun)run: in case you want to calluploadchannelyourself
Args, options, and kwargs
There are three types of arguments involved in a chef run:
args(dict): command line args as parsed by the sushi chef class and its parentsSushiChef: the
SushiChef.__init__method configures argparse for the following:compress,download_attempts,prompt,publish,resume,stage,step,thumbnails,token,update,verbose,warn
MySushiChef: the chef’s
__init__method can define additional cli args
options(dict): additional [OPTIONS…] passed at the end of the command lineoften used to pass the language option (
./sushichef.py ... lang=fr)
kwargs(dict): chef-specific keyword arguments not handled by ricecooker’suploadchannelmethodthe chef’s
runmethod makes the calluploadchannel(self, **args.__dict__, **options)while the definition ofuploadchannellooks likeuploadchannel(chef, verbose=False, update=False, ... stage=False, **kwargs)sokwargscontains a mix of bothargsandoptionsthat are not explicitly expected by theuploadchannelfunctionThe function
uploadchannelwill pass**kwargson to thechef’sget_channelandconstruct_channelmethods as part of the chef run.