I'm new to click, and have reached the first thing I don't understand.
I'm trying to create a @click.argument()
that has the required
tag to be dependent on a @click.option()
.
I made some code for testing this out.
@click.command()
@click.option('-t','--test',is_flag=True, default=False)
@click.argument('source', default=None, required=False)
def foo(source, test):
if source:
print('source is something')
print(test)
else:
print('source is None')
print(test)
if __name__ == '__main__':
foo()
But I want you have @click.argument('source', default=None, required=False)
to be required only if the option -t
is given. Otherwise, it should accept not having the argument.
I tried doing things like this: @click.argument('source', default=None, required=test)
but it didn't work. The variable test
seems to work like a normal variable in the foo
function but it doesn't seem to be defined outside of it.
I have found threads where people make custom classes. I'm very overwhelmed with this. I seem to be able to only find custom classes for options that are required based on another option; not arguments. I'm hoping for an easier solution, but if I had to use a custom class, what might it look like?
Any help would be greatly appreciated.