Custom arguments' help is not shown when calling `locust --help`
Prerequisites
- [X] I am using the latest version of Locust
- [X] I am reporting a bug, not asking a question
Description
(I'm not certain you would consider this a bug or a feature request.)
How to reproduce
- Create a
locust.conffile in the current directory - Add custom arguments
- Run
locust --help(or evenlocust -f locustfile.py)
Expected result
- I expect
locustto load thelocustfile.pyand run theinit_command_line_parserhook - I expect to see my custom arguments' help to be shown in
locust --help's output
Actual result
- The
locustfile.pyis not loaded at all when runninglocust --help. - The custom arguments' help are not shown in
locust --help's output.
Possible fix
Replace the built-in help action by something that loads the locustfile before printing the help.
- Add an explicit
--helpargument (parser.add_argument('-h', '--help', action="store_true" ...) - Check if
args.help == True - Call
parser.print_help()andparser.exit()(just like the default_HelpActiondoes)
Command line
locust --help
Locustfile contents
import argparse
from locust import events
print("top-level")
@events.init_command_line_parser.add_listener
def init_command_line_parser(parser: argparse.ArgumentParser):
"""
Initialize the command line parser with custom arguments.
"""
print("init_command_line_parser")
parser.add_argument(
"--my-little-argument",
type=str,
help="Choose a custom load shape",
choices=["a", "b", "c"],
)
Python version
Test with both Python 3.10.12 and 3.12.6
Locust version
2.31.6
Operating system
Ubuntu 22.04.4 LTS and 24.04 LTS
Yea this is a real bug. I think your suggested fix sounds reasonable and would love for you to PR it!
@fstamour is anyone working this ? , would like to have a crack at it
I would like to work on this if there's any chance..? @cyberw
Go for it. I meant to assign it to you.
Hey @cyberw @fstamour I was having some trouble to set up this project locally but after a succcessful setup now I am stuck with how to get to the fix.. The possible fix provided when executed refers to a conflict addressing the pre-existence of a default help argument.. If you can maybe elaborate further on the possible fix, I'll get it working
- Python's argparse, by default, handles the "--help" argument automatically.
- Argparse will print the help message and exit the process when calling "parse argument", which means the process exits before locust can even look at the other arguments
- But locust must look at the other arguments to be able to load the locustfile and execute the hook that will add the custom arguments before the help is shown and the process exits
- By manually adding a custom arguments "--help", it overrides the default behaviour of printing the help and exiting the process
- That way, locust can process the other arguments, load the file, and execute the hook
- Once that is done, locust needs to do what the default handler was doing, so it must check the arguments to see if "--help" was passed, and if it is, then call
parser.print_help()to print the help and then locust needs to exit the process
@shauryapanchal make sense?
This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 10 days.
Don't close, this would still be pretty useful
This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 20 days.
this is annoying...
This issue was closed because it has been marked stale for 20 days with no activity. This does not necessarily mean that the issue is bad, but it most likely means that nobody is willing to take the time to fix it. If you have found Locust useful, then consider contributing a fix yourself!
So my previous comment didn't count as "activity" 😡