From c26b00853633c7df8cdee0ee49b3596154bb09c1 Mon Sep 17 00:00:00 2001 From: kotfu Date: Fri, 12 Jan 2018 22:28:29 -0700 Subject: new @with_argument_list decorator --- examples/argparse_example.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) (limited to 'examples/argparse_example.py') diff --git a/examples/argparse_example.py b/examples/argparse_example.py index 3574f549..40ea372c 100755 --- a/examples/argparse_example.py +++ b/examples/argparse_example.py @@ -14,7 +14,7 @@ verifying that the output produced matches the transcript. import argparse import sys -from cmd2 import Cmd, make_option, options, with_argument_parser +from cmd2 import Cmd, make_option, options, with_argument_parser, with_argument_list class CmdLineApp(Cmd): @@ -69,10 +69,20 @@ class CmdLineApp(Cmd): argparser.add_argument('content', nargs='+', help='content to surround with tag') @with_argument_parser(argparser) def do_tag(self, arglist, args=None): - self.stdout.write('<{0}>{1}'.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 + """create a html tag""" + self.poutput('<{0}>{1}'.format(args.tag[0], ' '.join(args.content))) + + + @with_argument_list + def do_tagg(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}'.format(tag, ' '.join(content))) + else: + self.perror("tagg requires at least 2 arguments") + # @options uses the python optparse module which has been deprecated # since 2011. Use @with_argument_parser instead, which utilizes the -- cgit v1.2.1