Giter Club home page Giter Club logo

Comments (2)

BrianPugh avatar BrianPugh commented on August 27, 2024 1

thanks for the minimal example! Initially this appears to be a bug, but upon closer inspection it's actually intended behavior.

Cyclopts parses items in order:

  1. Attempts to parse all keyword/value pairs and flags.
  2. Attempts to parse positional arguments from all remaining tokens.

So, in the first example, the --is-set gets assigned to the is_set python variable. --something-else doesn't have a valid keyword assignment, so it gets passed along as a positional argument. This leaves --something-else to be parsed to the next availabe positional parameter, which is tokens.

In the second example, --something-else doesn't have a valid keyword assignment, so it gets passed along as a positional argument. The first positional argument is now is_set, which has the default annotated Parameter(allow_leading_hyphen=False), leading to the UnknownOptionError .

In order to get your desired behavior, it's probably better to:

@app.default
def main(*tokens: Annotated[str, Parameter(show=False, allow_leading_hyphen=True)], is_set: bool = False):
    print(f"is_set = {is_set}")
    print(f"extras = {tokens}")

which then works as you'd expect.

from cyclopts.

timpatt avatar timpatt commented on August 27, 2024 1

Thanks so much for such a quick and helpful response, @BrianPugh!

I had previously thought that the *tokens field was somehow handled specially, but now I see it actually isn't - it's just a standard, required (but possibly empty) list of positional arguments. Thanks again!

Thanks for an awesome library; this is the forth I've tried (argparse, fire, pyinvoke, click) and is the first I'm really happy with!

from cyclopts.

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.