Is it possible to add options dynamicly from a valid list/tuple? #2654
Unanswered
m986883511
asked this question in
Q&A
Replies: 2 comments 1 reply
-
I found this is test ok, code is shorter, but option is valid. @nova.command()
@click.option('--cpu_allocation_ratio')
@click.option('--ram_allocation_ratio')
@click.option('--disk_allocation_ratio')
@click.option('--vcpu_pin_set')
@click.option('--reserved_host_memory_mb')
def config_host_nova_settings(*args, **kwargs):
update_dict = {key:value for key, value in kwargs.items() if value is not None}
value = business.NovaEndPoint().config_host_nova_settings(ctxt={}, config=update_dict)
assert isinstance(value, dict), f'return value should be dict, but is {type(value)}'
value = json.dumps(value, indent=4)
click.secho(value, fg='green') |
Beta Was this translation helpful? Give feedback.
0 replies
-
I have a very similar scenario, converting from def main():
parser = argparse.ArgumentParser()
parser.add_argument("--foo", action="store_true")
for switch, name in [("a", "1"), ("b", "2"), ("c", "3"), ("d", "4"), ("e", "5")]:
parser.add_argument(
f"--{switch}days", action="store", type=float, help=f"Do something {name}"
)
parser.add_argument("--bar", action="store_true") What I currently have is pretty cringe: def _days(f):
for switch, name in reversed([("a", "1"), ("b", "2"), ("c", "3"), ("d", "4"), ("e", "5")]):
click.option(
f"--{switch}days", action="store", type=float, help=f"Do something {name}"
)(f)
return f
@click.command()
@click.option("--foo")
@_days
@click.option("--bar")
def main() |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
default_need_change_config_list may be more than 5 item, the code is too long.
I think it redundant and cumbersome. Is there a better way?
like:
Beta Was this translation helpful? Give feedback.
All reactions