diff options
| author | kotfu <kotfu@kotfu.net> | 2018-01-12 23:11:53 -0700 |
|---|---|---|
| committer | kotfu <kotfu@kotfu.net> | 2018-01-12 23:11:53 -0700 |
| commit | 405f4e4e951e0af46c7c5d746459f24e5c316eab (patch) | |
| tree | 7933766506292f7610d5b05aa5f6615b32e2bff7 /examples/arglist_example.py | |
| parent | c26b00853633c7df8cdee0ee49b3596154bb09c1 (diff) | |
| download | cmd2-git-405f4e4e951e0af46c7c5d746459f24e5c316eab.tar.gz | |
add use_argument_list setting
new attribute on Cmd2.cmd which defaults to false, but if set true, causes all do_* commands to receive a list of arguments, instead of a string of what the user typed.
Diffstat (limited to 'examples/arglist_example.py')
| -rwxr-xr-x | examples/arglist_example.py | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/examples/arglist_example.py b/examples/arglist_example.py new file mode 100755 index 00000000..5025c4be --- /dev/null +++ b/examples/arglist_example.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python +# coding=utf-8 +"""A sample application for cmd2 showing how to use argparse to +process command line arguments for your application. + +Thanks to cmd2's built-in transcript testing capability, it also +serves as a test suite for argparse_example.py when used with the +exampleSession.txt transcript. + +Running `python argparse_example.py -t exampleSession.txt` will run +all the commands in the transcript against argparse_example.py, +verifying that the output produced matches the transcript. +""" +import argparse +import sys + +from cmd2 import Cmd, make_option, options, with_argument_parser, with_argument_list + + +class CmdLineApp(Cmd): + """ Example cmd2 application. """ + def __init__(self): + self.use_argument_list = True + Cmd.__init__(self) + + def do_tag(self, arglist): + """verion of creating an html tag using arglist instead of argparser""" + if len(arglist) >= 2: + tag = arglist[0] + content = arglist[1:] + self.poutput('<{0}>{1}</{0}>'.format(tag, ' '.join(content))) + else: + self.perror("tag requires at least 2 arguments") + +if __name__ == '__main__': + # Instantiate your cmd2 application + c = CmdLineApp() + + # And run your cmd2 application + c.cmdloop() |
