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.

Unandled exception if ArgparseController is missing default function

See original GitHub issue

Using ArgparseController without a default command results in an unhandled exception.

Code:

from cement.core.foundation import CementApp
from cement.ext.ext_argparse import ArgparseController, expose


class MyBaseController(ArgparseController):
    @expose(help='<Subcommand description>')
    def subcommand(self):
        self.app.log.info("subcommand successful")


class MyApp(CementApp):
    class Meta:
        label = 'demo'
        handlers = [MyBaseController]


with MyApp() as app:
    app.run()

The help output looks as expected:

> python3 demo4.py --help
usage: demo [-h] [--debug] [--quiet] {subcommand} ...

optional arguments:
  -h, --help    show this help message and exit
  --debug       toggle debug output
  --quiet       suppress all output

sub-commands:
  {subcommand}
    subcommand  <Subcommand description>

But running with no command raises an exception:

> python3 demo4.py
Traceback (most recent call last):
  File "demo4.py", line 18, in <module>
    app.run()
  File "C:\Python34\lib\site-packages\cement\core\foundation.py", line 882, in run
    return_val = self.controller._dispatch()
  File "C:\Python34\lib\site-packages\cement\ext\ext_argparse.py", line 927, in _dispatch
    (contr.__class__.__name__, func_name))      # pragma: nocover
cement.core.exc.FrameworkError: Controller function does not exist MyBaseController.default()

It seems that, if there is no default function, the best thing would be to output a parse error, as if the user had typed in an invalid function.

Otherwise, I take it that the default function is required by cement, in which case a doc update might be appropriate: http://builtoncement.com/2.10/api/ext/ext_argparse.html#cement-ext-ext-argparse

Thoughts?

(Obviously a good workaround is to create a default command and print the help text.)

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:14 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
leefowlercucommented, Mar 20, 2017

@derks I can confirm that @expose(hide=True) does hide the commands from the sub-commands: list but still shows them both on the usage line:

usage: conduit [-h] [--debug] [--quiet] {default,test-command} ...

and on the sub-commands shortlist:

sub-commands:
  {default,test-command}

I’d argue that for every other command other than default this is probably the intended behavior. But, since the default command is enforced by the framework, the default command showing those places is confusing clutter. Users will almost always invoke a command via <command> as opposed to <command> default.

Having it show in those lists is both confusing from a user-perspective and aesthetically displeasing from a developer perspective.

Edit: My intended use-case for cement is to produce a CLI application with two different modes of operation, and as such, I don’t want a default implementation for the base command (other than printing usage information). The usage scenario would be:

<application> -application_options <mode_1> -mode_1_options and <application> -application_options <mode_2> -mode_2_options.

Enforcing an implementation of the default command on the base controller makes this extremely difficult, and again, clutters the usage info with the irrelevant default command

1reaction
derkscommented, Feb 6, 2017

@jtpereyda as an aside, you can use @expose(hide=True) so that the default (or other functions) don’t show in --help.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What type of error argparse.ArgumentParser raises for missing ...
As default argparse doesn't throw an exception, but exit when an error occur. Since python 3.9, there is an exit_on_error argument, ...
Read more >
argparse — Parser for command-line options, arguments and ...
If the function raises ArgumentTypeError , TypeError , or ValueError , the exception is caught and a nicely formatted error message is displayed....
Read more >
Cement Framework - API Reference
Cement is an advanced CLI Application Framework for Python. Its goal is to introduce a standard, and feature-.
Read more >
How to handle invalid arguments with argparse in Python?
The program given below takes two arguments – uname, pwd (user name, password). Here the criteria for setting username is, it should have...
Read more >
Bug listing with status RESOLVED with resolution OBSOLETE ...
... Bug:137479 - "[java-experimental] Velocity app missing when emerging dev-java/jboss-module-varia-4.0.2" status:RESOLVED resolution:OBSOLETE severity: ...
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