question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Global optional arguments

See original GitHub issue

Hi again,

I have 2 controller: Base and Parser. In Base I have:

class BaseController(controller.CementBaseController):

    class Meta:
        label = 'base'
        description = "application"
        config_defaults = dict(
            debug = False,
            foo='bar', some_other_option='my default value',
        )

        arguments = [
            (['-f', '--foo'], dict(action='store')),
            (['--log-level'],
                dict(action='store', dest='level',
                    choices=('debug', 'info', 'warn', 'error', 'critical'),
                    help='Change logging level')),

        ]

    @controller.expose(hide=True, aliases=['run'])
    def default(self):
        logger.error("Test error")
        logger.info("Test info")

Thosre are the allowed arguments:

python application.py --help                                                                                                                                                          1 ↵
usage: application.py (sub-commands ...) [options ...] {arguments ...}

application

commands:

  parser
    Parser controller

optional arguments:
  -h, --help            show this help message and exit
  --debug               toggle debug output
  --quiet               suppress all output
  -f FOO, --foo FOO
  --log-level {debug,info,warn,error,critical}
                        Change logging level

Especially the log-level argument should be passed “globally” to all other controller. However when I try to do that, I get:

$ python application.py parser.py --log-level info
usage: application.py (sub-commands ...) [options ...] {arguments ...}
application.py: error: unrecognized arguments: parser.py

The other way around doesn’t work as well:

python application.py --log-level info parser       
usage: application.py (sub-commands ...) [options ...] {arguments ...}
application.py: error: unrecognized arguments: parser

Am I missing sth? Thx in advance.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
derkscommented, Jun 20, 2015

This has now been refactored into the new ArgparseController that will eventually replace CementBaseController.

See: http://cement.readthedocs.org/en/latest/api/ext/ext_argparse/

Essentially, rather than having to duplicate the same option onto every controller… such as ‘log-level’, the controller now honors options throughout the entire change. Meaning, a global option of --log-level belongs on the base controller, and would be passed as:

$ myapp --log-level ERROR [some-controller] --some-other-option {some-command}

The ArgparseController provides the ability for all controllers to handle their arguments (or other arguments) regardless of which controller/function is actually being dispatched.

Example:

class Base(ArgparseController):
    class Meta:
        label = 'base'
        arguments = [
            (['--log-level'], 
             dict(help='modify the log level', dest='log_level', action='store')),
        ]

    def _post_argument_parsing(self):
        # this gets called in every controller after argument parsing, before command execution
        if self.app.pargs.log_level:
            self.app.log.set_level(self.app.pargs.log_level)

The above is just an example, it’s untested… but provided to simply follow up with this discussion. Please see the doc on the new capabilities of cement.ext.ext_argparse.ArgparseController.

0reactions
dorneanucommented, Jan 28, 2015

Your solution worked like a charm! 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Python Optional Arguments When Defining Functions
In this tutorial, you'll learn about Python optional arguments and how to define functions ... Using global variables in this way is not...
Read more >
Named and Optional Arguments - C# Programming Guide
Optional arguments enable you to omit arguments for some parameters. Both techniques can be used with methods, indexers, constructors, ...
Read more >
global variable as default argument in python function
I am trying to write a python function with an optional argument tr, if this argument was not specified, then I want to...
Read more >
Global arguments (optional) - CloudBees Documentation
Global arguments supply general information quickly, including cmtool online help.
Read more >
optional arguments to tikzpicture to set style globally - TeX
The reason is that you have specified the foo style with a default key which kicks in if none given. What you need...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found