GithubHelp home page GithubHelp logo

Comments (5)

neithere avatar neithere commented on July 26, 2024

This is by design

Please note how the pythonic notation maps to that of argparse:

  • f(x) = arg('x') = positional
  • f(x=y) = arg('--x', default=y) = optional (keyword)

You have defined a positional argument in your function signature and then refer to an optional argument (kwarg) which does not exist.

Let's define a command with both types of arguments with both "natural" and "explicit" notations:

#!/usr/bin/env python
import argh

@argh.arg('my-positional', help='a positional argument')
@argh.arg('--my-optional', help='an optional argument')
def mycommand(my_positional, my_optional='full'):
    print(my_positional, my_optional)

argh.dispatch_command(mycommand)

Run it:

$ python issue47.py foo
foo full

There's room for improvement

To sum up, there's no bug here but the confusion is certainly undesired.

I think we should raise a more informative exception if the @arg decorator refers to an argument of one type whereas an argument of different type exists with such name. Something like "optional argument --my-switch conflicts with positional my-switch from function signature".

from argh.

neithere avatar neithere commented on July 26, 2024

Changed the issue title from "How do include optional arguments with hyphens/underscores" to "More informative exception on explicit vs inferred arg type clash".

from argh.

redstreet avatar redstreet commented on July 26, 2024

Thanks for the explanation, it makes much more sense now. For some reason, I didn't get this from the documentation including the tutorial part, so perhaps adding this example or something similar might help.

BTW, thanks for argh. It is simply fantastic to use, and I really like the DRY, pythonic design.

from argh.

neithere avatar neithere commented on July 26, 2024

I've started writing tests for this issue and found out that the problem actually does not appear until there's a underscore/hyphen in argument name. If there isn't one, the application still works properly but another subtle bug may appear:

import argh

@argh.arg('--foo', default=1)
def f(foo):
    "Positional in signature, keyword in decorator"
    return foo

@argh.arg('foo')
def p(foo=1):
    "Keyword in signature, positional in decorator"
    return foo

argh.dispatch_commands([f, p])

and the call:

$ python issue47.py p 5
5
$ python issue47.py p
usage: issue47.py p [-h] foo
issue47.py p: error: the following arguments are required: foo

(Note that whether it may be OK to override some features of the original signature with decorators, here the very nature of the argument is distorted in a seemingly harmless and simple piece of code.)

So:

  1. Underscores should be always converted to hyphens before comparison of inferred and declared arguments;
  2. Assembling should ensure strict mutual conformance of definitions. This means that any combination of equally named positional arguments and flags should raise an error with an easily understandable message.

from argh.

redstreet avatar redstreet commented on July 26, 2024

Works great, thanks.

from argh.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.