How to Include the Naked Command Line Parser in Your Project

The command line parser is found in the Naked.commandline module. To import it, add the following code to your file:

import sys
import Naked.commandline

and then create a new instance of the Command object like this:

c = Naked.commandline.Command(sys.argv[0], sys.argv[1:])

Your command object contains attributes for the positional arguments in the command string:

c.cmd   #Primary command
c.cmd2  #Secondary command
c.arg_to_cmd  #first positional argument to the primary command
c.arg_to_cmd2  #first positional argument to the secondary command
You can test for options & flags with methods:
c.option(option_string, [bool argument_required])  #test for option with optional positional argument to option test
c.option_with_arg(option_string)  #test for option and mandatory positional argument to option
c.flag(flag_string)  #test for presence of a "--option=argument" style flag
And the argument strings are returned with methods:
c.arg(arg_string)  #returns the next positional argument to the arg_string argument
c.flag_arg(flag_string)  #returns the flag assignment for a "--option=argument" style flag
Advertisements