Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom arguments' help is not shown when calling locust --help #2917

Open
2 tasks done
fstamour opened this issue Sep 27, 2024 · 6 comments
Open
2 tasks done

Custom arguments' help is not shown when calling locust --help #2917

fstamour opened this issue Sep 27, 2024 · 6 comments
Assignees
Labels
bug hacktoberfest See https://hacktoberfest.digitalocean.com for more info

Comments

@fstamour
Copy link

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 fstamour added the bug label Sep 27, 2024
@cyberw
Copy link
Collaborator

cyberw commented Oct 13, 2024

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

@cyberw cyberw added the hacktoberfest See https://hacktoberfest.digitalocean.com for more info label Oct 13, 2024
@shubhamsugara22
Copy link

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

@shauryapanchal
Copy link

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

@cyberw
Copy link
Collaborator

cyberw commented Oct 22, 2024

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

@shauryapanchal
Copy link

shauryapanchal commented Oct 26, 2024

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

@fstamour
Copy link
Author

fstamour commented Nov 2, 2024

  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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug hacktoberfest See https://hacktoberfest.digitalocean.com for more info
Projects
None yet
Development

No branches or pull requests

4 participants