Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>While the <code>argparse</code> answer was accepted, the original question was how to accomplish this with the simpler <code>argh</code> module. Here's how:</p> <pre><code>from argh import * from show import show # only needed for demonstration purposes CMD_ALIASES = { 'dpd' : 'download-and-parse-data', 'dp' : 'download-and-parse-data', 'dd' : 'download-data', 'pd' : 'parse-data', } def choices_for(d): """ Return list of unique items (command aliases plus unabridged commands). Works in Python &gt;= 2.5. NB In the degenerate case when an alias and an unabridged command are identical, the alias takes priority. (In my best Terminator voice: Don't do dat.) """ return sorted(set(d.keys()) | set(d.values())) @arg('cmd', choices=choices_for(CMD_ALIASES)) @arg('-r', '--randomize-order', default=False) @arg('-t', '--training-percent', default=0) def main(args): args.command = CMD_ALIASES.get(args.cmd, args.cmd) show(args, props=True) dispatch_command(main) </code></pre> <p>This sets up a slightly generalized aliasing mechanism (one that allows multiple aliases for any given command). The resulting <code>args</code> object has both an <code>args.cmd</code> value for the command actually given, and a canonical <code>args.command</code> value that resolves the given alias, if any.</p> <p>For example:</p> <pre><code>$ ./my_program.py dpd -r -t 80 args: cmd='dpd' command='download-and-parse-data' randomize_order=True training_percent=80 </code></pre> <p>There is also another mechanism built into <code>argh</code> to do command aliases if you are dispatching commands to individual functions: the <code>@aliases</code> decorator, described <a href="http://argh.readthedocs.org/en/latest/reference.html" rel="nofollow">here</a>. NB this approach has a dependency on recent versions of <code>argparse</code>, so may not work in older Python distributions/environments.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload