Comments (6)
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.
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.
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.
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.
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.
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)
- migration guides? HOT 2
- support for union of None like Optional HOT 3
- App() name parameter type hint adjustment HOT 2
- Docstring hyperlink HOT 11
- [Feature request]: Nested pydantic validation HOT 9
- [Discussion] First-class config overrides via pyproject.toml? HOT 1
- [Feature Request] Completion? HOT 6
- App() console parameter HOT 2
- integration with https://pypi.org/project/typed-settings/ HOT 2
- [BUG]: bad error report because of assetion error when there is UnusedCliTokensError HOT 2
- [BUG] Weird behavior of list of tuples HOT 7
- [BUG] list of tuple of single item is parsed but take only the first character of each token get parsed HOT 2
- Misleading error message when flag name has an underscore HOT 2
- the help format parsing is not consistent HOT 1
- Misc likely (rare) typing bugs HOT 1
- Use of the meta application results in a double help display HOT 5
- Automatic conversion from underscore to hyphen HOT 3
- Newlines should not be present in descriptions that span multiple lines HOT 4
- [Feature Request] Add auto_env_var_prefix HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cyclopts.