summaryrefslogtreecommitdiff
path: root/examples/argparse_example.py
diff options
context:
space:
mode:
authorkotfu <kotfu@kotfu.net>2018-01-07 14:12:57 -0700
committerkotfu <kotfu@kotfu.net>2018-01-07 14:12:57 -0700
commitc25a2b7949c02449279f548db1c8de9d10214cdc (patch)
treea5ae8d39b04c50d37bc178308e2ccf2d56834f02 /examples/argparse_example.py
parent49bf448d2f5833a6a8710a92ed7a6cf6228f6c0e (diff)
downloadcmd2-git-c25a2b7949c02449279f548db1c8de9d10214cdc.tar.gz
Add tests for POSIX=true and arguments containing spaces
Diffstat (limited to 'examples/argparse_example.py')
-rwxr-xr-xexamples/argparse_example.py20
1 files changed, 18 insertions, 2 deletions
diff --git a/examples/argparse_example.py b/examples/argparse_example.py
index 96f8118d..16de343c 100755
--- a/examples/argparse_example.py
+++ b/examples/argparse_example.py
@@ -59,6 +59,10 @@ class CmdLineApp(Cmd):
# self.stdout.write is better than "print", because Cmd can be
# initialized with a non-standard output destination
+ do_say = do_speak # now "say" is a synonym for "speak"
+ do_orate = do_speak # another synonym, but this one takes multi-line input
+
+
argparser = argparse.ArgumentParser(
prog='sspeak',
description='Repeats what you tell me to'
@@ -83,8 +87,20 @@ class CmdLineApp(Cmd):
self.stdout.write('\n')
# self.stdout.write is better than "print", because Cmd can be
# initialized with a non-standard output destination
- do_say = do_speak # now "say" is a synonym for "speak"
- do_orate = do_speak # another synonym, but this one takes multi-line input
+
+
+ argparser = argparse.ArgumentParser(
+ prog='tag',
+ description='create an html tag, the first argument is the tag, the rest is the contents'
+ )
+ argparser.add_argument('tag', nargs=1, help='tag')
+ argparser.add_argument('content', nargs='+', help='content to surround with tag')
+ @with_argument_parser(argparser)
+ def do_tag(self, cmdline, args=None):
+ self.stdout.write('<{0}>{1}</{0}>'.format(args.tag[0], ' '.join(args.content)))
+ self.stdout.write('\n')
+ # self.stdout.write is better than "print", because Cmd can be
+ # initialized with a non-standard output destination
if __name__ == '__main__':