locust icon indicating copy to clipboard operation
locust copied to clipboard

Custom arguments' help is not shown when calling `locust --help`

Open fstamour opened this issue 1 year ago • 2 comments

Prerequisites

Description

(I'm not certain you would consider this a bug or a feature request.)

How to reproduce

  1. Create a locust.conf file in the current directory
  2. Add custom arguments
  3. Run locust --help (or even locust -f locustfile.py)

Expected result

  • I expect locust to load the locustfile.py and run the init_command_line_parser hook
  • I expect to see my custom arguments' help to be shown in locust --help 's output

Actual result

  • The locustfile.py is not loaded at all when running locust --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 --help argument (parser.add_argument('-h', '--help', action="store_true" ...)
  • Check if args.help == True
  • Call parser.print_help() and parser.exit() (just like the default _HelpAction does)

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

fstamour avatar Sep 27 '24 13:09 fstamour

Yea this is a real bug. I think your suggested fix sounds reasonable and would love for you to PR it!

cyberw avatar Oct 13 '24 17:10 cyberw

@fstamour is anyone working this ? , would like to have a crack at it

shubhamsugara22 avatar Oct 14 '24 07:10 shubhamsugara22

I would like to work on this if there's any chance..? @cyberw

shauryapanchal avatar Oct 22 '24 09:10 shauryapanchal

Go for it. I meant to assign it to you.

cyberw avatar Oct 22 '24 18:10 cyberw

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

shauryapanchal avatar Oct 26 '24 11:10 shauryapanchal

  1. Python's argparse, by default, handles the "--help" argument automatically.
  2. 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
  3. 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
  4. By manually adding a custom arguments "--help", it overrides the default behaviour of printing the help and exiting the process
  5. That way, locust can process the other arguments, load the file, and execute the hook
  6. 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?

fstamour avatar Nov 02 '24 15:11 fstamour

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.

github-actions[bot] avatar Jan 02 '25 02:01 github-actions[bot]

Don't close, this would still be pretty useful

fstamour avatar Jan 02 '25 12:01 fstamour

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.

github-actions[bot] avatar Mar 04 '25 02:03 github-actions[bot]

this is annoying...

fstamour avatar Mar 06 '25 15:03 fstamour

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!

github-actions[bot] avatar Mar 27 '25 02:03 github-actions[bot]

So my previous comment didn't count as "activity" 😡

fstamour avatar Mar 28 '25 03:03 fstamour