This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Oz.Tiram
Recipients Oz.Tiram
Date 2018-04-05.06:26:52
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1522909613.34.0.682650639539.issue33227@psf.upfronthosting.co.za>
In-reply-to
Content
Considering that I want to build an CLI interactive program using cmd.Cmd,
which has multiple functions that accept two or more arguments,
I always have to do parsing of arg inside my do_methods for example:

class Calc(cmd.Cmd):

    do_add(self, arg):
       a, b = parse_ints(arg)
       return a+b

    do_mult(self, arg):
       a, b = parse_ints(arg)
       return a*b

This is easy enough for a simple calculator. But for more logic I need to create more parse_type and explicitly call them at the top of each do_stuff.
Considering this I have created a patch for Cmd.cmd, that keeps the old functionality, but adds the ability to pass multiple arguments without having to parse them every time. 
  
The functionality is as follows:


class MyProg(cmd.Cmd):

    do_simple(self, arg):
       print(arg)

    do_greet(self, name="World")
       print("Hello %s" %s)

    do_add(self, a, b):
       print(a+b)
    

Now in the cmd prompt:

(Cmd) simple foo
foo  
(Cmd) # keeps the old functionallity 
(Cmd) add 2 3
5
# add two properly add the values
(Cmd) add 2
# expects two arguments, so it will print the help for add
(Cmd) greet
Hello World
# does not complain because it has a default value
(Cmd) greet Guido
Hello Guido
(Cmd) greet name=Raymond
Hello Raymond
# works too

None of these example guess the type. It is simply handles as a string. but I am playing around with automatically detecting types of variables using function annotations in python 3.5 and later.

Also, although, not demonstrated here, one can also define:

    def do_some_method_with_kwargs(self, arg1, arg2, **kwargs):
        ... do fancy stuff ...

(Cmd) some_method_with_kwargs foo bar kwargs='{"name": "John", "age"=35}'

kwargs is parsed to a dict (using json.loads) when escaped properly.
This is a bit esoteric, hence not shown here (but the patch patch includes it).
 
I was wondering if this kind of enhancement would be considered for this module? 
I will happily create a PR with tests and documentation explaining this enhanced functionality.
History
Date User Action Args
2018-04-05 06:26:53Oz.Tiramsetrecipients: + Oz.Tiram
2018-04-05 06:26:53Oz.Tiramsetmessageid: <1522909613.34.0.682650639539.issue33227@psf.upfronthosting.co.za>
2018-04-05 06:26:53Oz.Tiramlinkissue33227 messages
2018-04-05 06:26:52Oz.Tiramcreate