I have locust script with the following argument parser:
# locustaction.py
@events.init_command_line_parser.add_listener
def parser_handler(parser):
parser.add_argument(
'--loadindex',
type=bool,
default=False,
help="Loads the index at init"
)
class MyUser(User):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.loadindex = self.environment.parsed_options.loadindex
print(f"self.loadindex={self.loadindex}")
I call this from the command line by:
locust --headless -f locustaction.py --loadindex=False
However, irrespective of whether I set --loadindex=False or --loadindex=True, inside locustaction.py it is always seen as True.
What am I doing wrong?
Tried with just
locust --headless -f locustaction.py --loadindex
but got the error message:
locust: error: argument --loadindex: expected one argument
When you define the argument using type=bool, it interprets any value provided (such as ‘False’ or ‘True’) as True because it only checks if the argument exists, not its value.