Global optional arguments
See original GitHub issueHi 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:
- Created 9 years ago
- Comments:5 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This has now been refactored into the new
ArgparseController
that will eventually replaceCementBaseController
.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 thebase
controller, and would be passed as: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:
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
.Your solution worked like a charm! 😃