Boolean argument passed to locust script not recognized

46 Views Asked by At

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
1

There are 1 best solutions below

2
TheHungryCub On

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.

# locustaction.py

@events.init_command_line_parser.add_listener
def parser_handler(parser):
    parser.add_argument(
        '--loadindex',
        action='store_true',  # Change to action='store_false' if you want the default to be False
        default=False,
        help="Loads the index at init"
    )