Skip to content Skip to sidebar Skip to footer

Python Click: Nosuchoption Exception When Manually Attaching Option Objects To Command Instance Using

My code sample: import click def std_cb(ctx, param, standardize): if standardize: opt = click.Option(param_decls=['-a'], help='this option

Solution 1:

I'm not sure if your code should work, but I wonder if you could live with something like I've sketched here:

import click


defrequire_standardize_set(ctx, param, value):                                                                                                                   
    if value andnot ctx.params['standardize']:
        raise click.UsageError('-{} requires that -S is set'.format(param.name))
    return value

defrequire_standardize_not_set(ctx, param, value):
    if value and ctx.params['standardize']:
        raise click.UsageError('-{} requires that -S is not set'.format(param.name))
    return value


@click.command()@click.option('-S', '--standardize/--no-standardize',
              is_flag=True, default=False, is_eager=True)@click.option('-a', help='this option requires that -S is set',
              callback=require_standardize_set)@click.option('-b', help='this option requires that -S is not set',
              callback=require_standardize_not_set)defget_options(standardize, **extra_opts):
    print(locals())


if __name__ == '__main__':
    uis = get_options.main(standalone_mode=False)

This seems to me to produce the same results (except extra_opts always includes both a and b, but with the values of None if not set). A benefit, from my point of view is that the documentation always documents both a and b. As a user, I assume I would want that.

Post a Comment for "Python Click: Nosuchoption Exception When Manually Attaching Option Objects To Command Instance Using"