| Home | Trees | Indices | Help |
|
|---|
|
|
Implements Lamson's command line argument parsing system. It is honestly
infinitely better than optparse or argparse so it will be released later as a
separate library under the BSD license.
It's used very easily. First, you write a module that is like lamson.commands.
Each function name BLAH_command implements a sub-command. Then you use
lamson.args.parse_and_run_command to parse the command line and run the function
that matches.
Note that the _command suffix is optional and configurable, but it is there
to disambiguate your commands so you can use Python reserved words and base
types as your command names. Without it, you can do a list_command or a
for_command.
You command then specifies its keyword arguments to indicate what has
reasonable defaults and what is required. Give a value to the option
to indicate its default, and give a None setting to indicate it is required.
A good way to read this is it is your commands "default settings" and None
says "this option has no default setting".
Here's an example from lamson:
def send_command(port=8825, host='127.0.0.1', debug=1, sender=None, to=None,
subject=None, body=None, file=False):
You can see this has subject, body, sender, and to as required options (they
are None), and the rest have some default value.
With this the argument parser will parse the users given arguments, and then
call your command function with those as keyword arguments, but after it has
fixed them up with the defaults you gave. In the event that a user does
not give a required option, lamson.args will abort with an error telling them.
Lamson's argument parser also accurately detects and parses integers, boolean
values, strings, emails, single word values, and can handle trailing arguments
after a -- argument. This means you don't have to do conversion, it should be
the right type for what you expect.
Lamson.args does not care if you use one dash (-help), two dashes
(--help), three dashes (---help) or a billion. In all honesty, who gives a
rat's ass, just get the user to type something like a dash followed by a word and
that's good enough.
If you just need argument parsing and no commands then you can just use
lamson.args.parse directly.
Finally, the help documentation for your commands is just the __doc__
string of the function.
|
|||
|
ArgumentError Thrown when lamson.args encounters a command line format error. |
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
SCANNER = re.Scanner([(r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-
|
|||
__package__ =
|
|||
|
|||
ip_address |
word |
|
option |
int |
bool |
empty |
string |
trailing |
Responsible for taking a token off and processing it, ensuring it is of the correct type. If of_type is None (the default) then you are asking for anything. |
Returns true if the next token is of the type, false if not. It does not modify the token stream the way match does. |
Parsing production that handles trailing arguments after a -- is given. |
The Option production, used for -- or - options. The number of - aren't important. It will handle either individual options, or paired options. |
List of options, optionally after the command has already been taken off. |
The command production, just pulls off a word really. |
Goes through the command line args and tokenizes each one, trying to match something in the scanner. If any argument doesn't completely parse then it is considered a 'string' and returned raw. |
Tokenizes and then parses the command line as wither a command style or
plain options style argument list. It determines this by simply if the
first argument is a 'word' then it's a command. If not then it still
returns the first element of the tuple as None. This means you can do:
command, options = args.parse(sys.argv[1:])
and if command==None then it was an option style, if not then it's a command
to deal with.
|
Uses the inspect module to figure out what the keyword arguments are and what they're defaults should be, then creates a dict with that setup. The results of determine_kwargs() is typically handed to ensure_defaults(). |
Goes through the given options and the required ones and does the work of making sure they match. It will raise an ArgumentError if any option is required. It will also detect that required TRAILING arguments were not given and raise a separate error for that. |
Takes a module, uses the command to run that function. |
Returns the dochelp from all functions in this module that have _command at the end. |
Returns the help string for just this one command in the module. If that command doesn't exist then it will return None so you can print an error message. |
Just returns the available commands, rather than the whole long list. |
Called when you give an invalid command to print what you can use. |
A one-shot function that parses the args, and then runs the command that the user specifies. If you set a default_command, and they don't give one then it runs that command. If you don't specify a command, and they fail to give one then it prints an error. On this error (failure to give a command) it will call sys.exit(1). Set exit_on_error=False if you don't want this behavior, like if you're doing a unit test. |
|
|||
SCANNER
|
__package__
|
| Home | Trees | Indices | Help |
|
|---|
| Generated by Epydoc 3.0.1 on Wed Jul 7 07:32:49 2010 | http://epydoc.sourceforge.net |