Coder Social home page Coder Social logo

Comments (6)

BrianPugh avatar BrianPugh commented on May 16, 2024

Cyclopts purposely limited its responsibility-scope to just the initial command CLI parsing. Other libraries are quite good at prompting the user for more data. My personal favorite is Questionary. With Questionary, your program may look like:

import questionary
from cyclopts import App

app = App()

@app.default
def main(password: Optional[str] = None):
    if password is None:
        password = questionary.password("What's your password?").ask()
    # Do stuff with the password here.

from cyclopts.

marcelbrueckner avatar marcelbrueckner commented on May 16, 2024

Oh, that was fast :) Thanks. I thought about that already, but then the nice "required" hint in the CLI's help isn't displayed anymore.

However, I've now experimented with the Parameters and discovered that I can set required=True.
This way (in combination with setting a default value) I have the corresponding hint in the CLI's help,
but without the error when the parameter isn't passed :)

So your code finally resembles to the following:

from cyclopts import App
# I will use rich instead of questionary because cyclopts depends on rich anyway
from rich.prompt import Prompt

app = App()

@app.default
def main(password: Annotated[Optional[str], Parameter(required=True)] = None):
    if password is None:
        password = Prompt.ask("What's your password?", password=True)
    # Do stuff with the password here.

from cyclopts.

BrianPugh avatar BrianPugh commented on May 16, 2024

Thanks for the use-case example!

However, I've now experimented with the Parameters and discovered that I can set required=True.
This way (in combination with setting a default value) I have the corresponding hint in the CLI's help,
but without the error when the parameter isn't passed :)

I think this behavior is technically a bug in Cyclopts's side, so I can't exactly recommend on relying on that behavior. I'll have to investigate more later when I have a moment. I'm going to keep this open until I provide some more feedback.

The docstring for required:

      Parameter must be supplied.
      Defaults to required if parameter does not have a default from the function signature.

I personally don't think it's a valid use-case to mark an item as required in the help-page if it's not actually required at invocation time. However I would definitely like to hear your opinion!

from cyclopts.

marcelbrueckner avatar marcelbrueckner commented on May 16, 2024

I personally don't think it's a valid use-case to mark an item as required in the help-page if it's not actually required at invocation time. However I would definitely like to hear your opinion!

Given the user wants to invoke my (future) tool in a non-interactive environment, the parameter needs to be passed at invocation time. Hence for me, that's "required" and I would like to express that in my CLI help in a visually coherent way.

For example, other (sub)commands of my tool will require non-password'ish parameters that I don't ask for interactively if missing. They will have the red "required" hint in CLI's help and I want the same for the password parameter.

from cyclopts.

BrianPugh avatar BrianPugh commented on May 16, 2024

I suppose the opposite (required=False without a default value being provided) makes even less sense.

Ok, I'm on board! I'm just going to keep this issue open until I update documentation and add tests that enforces this behavior. Thanks!

from cyclopts.

BrianPugh avatar BrianPugh commented on May 16, 2024

I updated the docs and fixed a minor related bug in #146. This is now part of the v2.5.1 release. Thank you!

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.