-
-
Notifications
You must be signed in to change notification settings - Fork 56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ability to Specify Global Arguments, Reduce Redunancy for Common Argument #63
Comments
I haven't yet found time to dig into this but here's something that can be related: http://sourceforge.net/p/pgbarman/code/ci/master/tree/barman/cli.py The Barman project uses Argh for CLI and:
I'm sure Argh should provide a documented way to do it but it may be better to gather use cases first. In my own CLI apps I stick to XDG + environ vars and that's usually enough. However, even these apps could be improved by a pre-dispatch setup function. WarningI must warn that the |
Another interesting example: https://github.com/alfredhq/alfred/blob/develop/alfred/__main__.py (the |
barman 1.3.3 seems to now break with argh 0.26 (pre_call seems to no longer work): https://sourceforge.net/p/pgbarman/tickets/56/ |
@ggaughan thanks, you are right. Fixed in 0.26.1. |
Smashing! Thank you. |
@neithere: that effectively promotes pre-call to API status. Is that intentional? |
Any news here? Some other use cases in addition to logging could be:
|
This is not in fact deprecation per se but merely an attempt to draw attention to a fact that was mentioned almost ten years ago in the discussion (#63): the `pre_call` argument in `dispatch()` is not a feature but a hack which is not recommended and will be removed. This commit makes it clear when precisely it is going to be removed and what actions are expected from the user.
FYI, the current hack ( Its approval would promote an approach which would be against the purpose of the library, i.e. keeping things clear, natural and "pythonic". Argh is primarily a method of automatically building CLI from Python function signatures. The There certainly should be a better, cleaner solution. Some ideas:
In any case, the fastest way to find a solution is to better define the problem. Gathering real use cases is perhaps the best way to move this issue forward. Perhaps there will be different solutions for seemingly identical problems. |
* feat: deprecate outdated constant * docs: list deprecated items * test: cover argh.assembling.add_subcommands() * test: cover overrides in add_commands() * test: 100% coverage for assembling and interaction * feat: deprecate `safe_input()` The function used to be helpful in the py2/py3 world with all the bytes vs Unicode strings mess. In 2023 this is not relevant anymore. * docs: deprecation of `dispatch(..., pre_call=...)` This is not in fact deprecation per se but merely an attempt to draw attention to a fact that was mentioned almost ten years ago in the discussion (#63): the `pre_call` argument in `dispatch()` is not a feature but a hack which is not recommended and will be removed. This commit makes it clear when precisely it is going to be removed and what actions are expected from the user. * test: 100% coverage of dispatching * chore: drop the old encoding declaration Let's just agree that it's 2023 and move on. * test: 100% coverage of completion * test: require 100% coverage
First, thank for fixing a few niggles after a long hiatus. I appreciate it. We love argh. We use We can't use a custom action for the global flag, because it won't get called if the flag is not on the command line, and sometimes the configuration is in an environment variable. Considering |
Thank you for your kind words about Argh, @mathieulongtin. Is that library open source? It would really help me to understand the use case if I could see the code. I don't want to leave you stuck with an unsupported version of Argh, but I need to be sure that I understand the problem well and there's no other way to address the issue. |
A bit more on this... Known workflowsThe normal workflow
The alfred workflowRather elegant but it drops one of the most powerful features of Argh — the mapping between CLI arguments and function signature.
The Barman workflow with
|
My use of def pre_call(ns):
global CONFIG_NAME
CONFIG_NAME = ns.config or os.environ.get("MYAPP_CONFIG")
if not CONFIG_NAME: raise RuntimeError(...)
load_config() The way I see it, configuration is environmental, the same as the current working directory or environment variables. Having pre-call do what you call "monkey patch", I call it "setting the environment in an acceptable way for the functions to execute". This is especially true for a CLI,. Moving along, there seems to be a need for ns = parse_args(parser)
# do what you'd do with pre-call
execute_function(parser, ns) From the argh perspective, this is clean. Any kind of side-effect is clearly from the code using argh, not within argh. |
Exceptions: * `pre_call` was scheduled for removal in v0.30 but it needs to be replaced with some other mechanism, see discussion in #63.
@mathieulongtin thanks for your input. |
@mathieulongtin does #193 look good for your use case? Please feel free to do the code review there or contribute the code. Targeting the upcoming release. |
Looks good! It would be hard to refuse the exact API change I asked for :) Thanks! |
Released in v0.30. |
This may be documentation bugs, and it might be the case that a few simple helper functions/decorators may resolve these issues:
First, some background: I really want some settings to be "global" in the sense that they're not passed to the function, exactly. For example, I want to set up logging, globally, and I don't want to have logging configuration in every command. I use a custom namespace class/object with a cusutom setter to correspond to the logging option so that this happens automatically, but there's no way to add an argument everywhere that I can fined without a lot of duplicated decorators in a or that's makes semantic sense (and is functional).
So the issues are:
@arg
variant like@arg_global
that just adds the argument to to the parser but doesn't pass the value to the wrapped function during dispatch, would be awesome.Thoughts?
The text was updated successfully, but these errors were encountered: