Comments (9)
Eager is only supposed to be used for things such as help or version or other parameters that have a similar behavior. Why do you need to set your parameter to eager?
from click.
Hmm. Maybe I'm abusing the feature. I'm trying to use it to set a higher priority for some parameters, so that a lower priority params can have access to the processed values of the eager params.
Here's an example that's closer to what I'm actually making. I was trying to use it to ensure that the train
option was processed before the test
option, as when the test
option is not supplied it is set equal to train
. I was trying to do that with callbacks, but I can also accomplish it in the function body.
import click
def train_to_list(ctx, value):
'''Convert TRAIN from a comma-separated list to
a true list.
To be supplied as a callback to click.'''
return value.split(',')
def grab_from_train(ctx, value):
return value or ctx.params['train'][0]
@click.command()
@click.option('-t', '--train',
help='A training set, or list of them. Eg. set1,set2,set3',
required=True,
is_eager=True, # process this before predict
callback=train_to_list)
@click.option('-p', '--predict',
help='If omitted, PREDICT will be the first entry in TRAIN.',
callback=grab_from_train)
def ml_cli(train,predict):
'''Train and test the a machine learning model.
'''
print 'train:', train
print 'predict:', predict
if __name__ == '__main__':
ml_cli()
$ python ml_cli.py -t set1
train: [u'set1']
predict: set1
$ python ml_cli.py -t set1 -p set3
train: [u'set1']
predict: set3
$ python ml_cli.py -t set1,set2,set3
train: [u'set1', u'set2', u'set3']
predict: set1
$ python ml_cli.py --help
Usage: ml_cli.py [OPTIONS]
Error: Missing option "-t / --train".
from click.
Parameters are handled in the order they are defined. As such you can just order them as you want to make that work.
from click.
Just to be clear about this: if you remove is_eager
your code continues working.
from click.
Ah. Well now I feel sheepish. 🐑
Thanks for the help :)
from click.
I have run into this issue as well. Parsing the argument "APPLICATION_ROOT" I retrieve a list of directories inside that path, and then the -l
parameter must be one of those dirs.
The way it is now, unless I specify a valid dir, I will not get any help text?
My code is:
application_dir = ''
voiceprompts_dir = ''
available_languages = []
def validate_and_set_paths(ctx, param, value):
global available_languages, application_dir, output_dir, voiceprompts_dir, voiceprompts_wav_dir, ignored_dirs
if not os.path.isdir(value):
logging.error('Provided parameter is not a directory'.format(value))
return False
else:
application_dir = os.path.abspath(value)
voiceprompts_dir = os.path.join(application_dir, 'voiceprompts')
if not os.path.isdir(voiceprompts_dir):
logging.error('Voiceprompts directory does not exist: {}'.format(voiceprompts_dir))
return False
dirnames = os.walk(voiceprompts_dir).next()[1]
for dirpath in dirnames:
#logging.info('dirpath: {}'.format(dirpath))
available_languages += [dirpath]
available_languages += ['ALL']
def validate_chosen_languages(ctx, param, value):
for language in value:
if language not in available_languages:
raise click.BadParameter('\n\tLanguage parameter provided ({}) must one of the following: {}'.format(language, available_languages))
return value
@click.command(context_settings=CONTEXT_SETTINGS)
@click.argument('application_root',
type=click.Path(exists=True),
callback=validate_and_set_paths,
required=True,
is_eager=True)
@click.option('--languages', '-l',
multiple=True,
default=['ALL'],
help='Language to convert',
callback=validate_chosen_languages)
def main(application_root, languages):
# Do stuff
pass
if __name__ == '__main__':
main()
from click.
@mitsuhiko I used same approach as @bgschiller, since user of script can change desired order of options which could change flow or break it. Is there another way how to define order of options instead of abusing is_eager
which is breaking the help now.
from click.
@s7anley: You need to preserve the order of your options if you want to use these options in other options callback function; see here. A workaround (if you like to call it as such) would simply be putting your callback function inside your command function and passing the context:
def baz_callback(ctx, baz):
if baz is None:
ctx.abort("Use baz!")
return baz.lower()
@click.command()
@click.option('--foo',
required=True)
@click.option('--bar')
@click.option('--baz')
@click.pass_context
def process_file(ctx, foo, bar, baz):
if bar is None:
bar = foo
baz = baz_callback(ctx, baz)
print(foo, baz)
Otherwise just use exceptions:
def foo_callback(ctx, _, value):
try:
bar = ctx.params['bar']
except KeyError:
ctx.abort("--bar must be used prior to --foo")
...
from click.
@skycaptain thanks for tips. I eventually did what I wanted in own decorator, which is executed last so, all the parameters provided by user are either there or not.
from click.
Related Issues (20)
- Missing return value in context manager example (Managing resources)?
- zsh completion still misbehaving in the presence of `:`
- Setting color=True in runner.invoke on windows strips ansi codes HOT 2
- Better i18n with gettext: use class-based API
- Broken code example in docs under Advanced Patterns
- Utility to introspect known command arguments HOT 3
- Autocorrect wrong/misspelled option with the suggested unique possibility. HOT 1
- Docs wrongly links PRs and Issues to flask
- The type hint for the filename parameter to open_file is incomplete HOT 1
- Missing reset on should_strip_ansi causing cascading issues during other tests
- Custom type called twice with prompt HOT 2
- Display exception notes when raising `ClickException` HOT 2
- Optionally expand global variables in parameters? HOT 2
- click.Path(resolve_path=True) crash on non-Path HOT 2
- partially initialized module 'click' has no attribute 'command' (most likely due to a circular import)
- Modifying default_map in callback behaves differently between click 7.1.2 and 8.0.0
- `flag_value` is not taken into account with `envvar`
- Click does not detect duplicate option flags HOT 2
- Cursor Keys show escape characters like ^[[D when inputting missing options HOT 2
- get_parameter_source does not work with invoke or forward
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 click.