Some inconsistent arguments with cement

I am writing a python command line interface tool on top of cement . Cement works well enough for standard argument analysis. However, I want to be able to add a certain number of arguments without a flag. Let me explain.

A typical command line tool:

cmd subcmd -flag -stored=flag

Now, let's say I want to add some arguments without flags, like how cd works

cd my/dir

my/dir is an argument without a flag.

Is there anyway to do this with cement?

My current cement application example:

# define application controllers
class MyAppBaseController(controller.CementBaseController):
    class Meta:
        label = 'base'
        interface = controller.IController
        description = "My Application Does Amazing Things"
        arguments = [
            (['--base-opt'], dict(help="option under base controller")),
            ]

    @controller.expose(help="base controller default command", hide=True)
    def default(self):
        self.app.args.parse_args(['--help'])
        print "Inside MyAppBaseController.default()"

    @controller.expose(help="another base controller command")
    def command1(self):
        print "Inside MyAppBaseController.command1()"

So let's say I wanted to do myapp command1 some/dir some_string

Is there any way to analyze these arguments?

+4
source share
2 answers

, ArgParse. GitHub , :

https://github.com/datafolklabs/cement/issues/256

, "command1", "some/dir some_string" . MyAppBaseController.Meta.arguments:

( ['extra_arguments'], dict(action='store', nargs='*') ),

, command:

if self.app.pargs.extra_arguments: print "Extra Argument 0: %s" % self.app.pargs.extra_arguments[0] print "Extra Argument 1: %s" % self.app.pargs.extra_arguments[1]

+10

Cement doc: "Cement IArgument, ArgParseArgumentHandler , ArgParse, Python"

, argparse , , pargs.

# define application controllers
class MyAppBaseController(controller.CementBaseController):
    class Meta:
        label = 'base'
        interface = controller.IController
        description = "My Application Does Amazing Things"
        arguments = [
            (['--base-opt'], dict(help="option under base controller")),
        ]

    @controller.expose(help="base controller default command", hide=True)
    def default(self):
        self.app.args.parse_args(['--help'])
        print "Inside MyAppBaseController.default()"

    @controller.expose(
        help="another base controller command",
        arguments=[
            (['path'],
             dict(type=str, metavar='PATH', nargs='?', action='store', default='.')),
            (['string'],
             dict(type=str, metavar='STRING', nargs='?', action='store', default='')),
        ]
    )
    def command1(self):
        print "Inside MyAppBaseController.command1() path: %s string: %s" % (self.app.pargs.name, self.app.pargs.string)
0

Source: https://habr.com/ru/post/1545897/


All Articles