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

Use click to parse arguments #2373

Merged
merged 3 commits into from
Apr 18, 2023
Merged

Use click to parse arguments #2373

merged 3 commits into from
Apr 18, 2023

Conversation

Swiftyos
Copy link
Contributor

Background

This PR changes the argument parsing library from argparse to click. The primary reason for this switch is to leverage the benefits provided by click, while maintaining backward compatibility with the existing implementation. Click is a powerful and flexible library for creating command-line interfaces, offering several advantages over argparse, such as a more intuitive and consistent API, better error messages, automatic generation of help messages, and advanced features like command chaining and nested commands.

Changes

  • Replace argparse with click for argument parsing.
  • Update the main function to use click decorators and options.
  • Renamed the parse_arguments to create_config
  • Ensure that existing functionality is preserved with minimal changes to the codebase.
  • Keep the door open for future enhancements using click's built-in features.

Benefits of Click

  • More intuitive and consistent API.
  • Better error messages and automatic generation of help messages.
  • Advanced features like command chaining and nested commands.
  • Actively maintained and widely used library, with extensive documentation and community support.

Documentation

The code changes include in-code comments to describe the new click decorators and options. The external documentation will be updated to reflect the change from argparse to click.

Test Plan

  1. Manually test the script with various command-line options to ensure that the existing functionality is preserved.
  2. Run existing test cases to ensure that the change does not introduce any regressions.

PR Quality Checklist

  • My pull request is atomic and focuses on a single change.
  • I have thoroughly tested my changes with multiple different prompts.
  • I have considered potential risks and mitigations for my changes.
  • I have documented my changes clearly and comprehensively.
  • I have not snuck in any "extra" small tweaks changes

@Swiftyos
Copy link
Contributor Author

Please see my previous attempt to get this in #1184

@Pwuts Pwuts changed the title Change Argument Parsing to click Use click to parse arguments Apr 18, 2023
Pwuts
Pwuts previously requested changes Apr 18, 2023
Copy link
Member

@Pwuts Pwuts left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice improvement to the CLI!

One small request: since there is only one command as of now, it's best if people can still run ./run.sh without any subcommands. Click seems to support this with click-default-group.

@Swiftyos
Copy link
Contributor Author

Nice improvement to the CLI!

One small request: since there is only one command as of now, it's best if people can still run ./run.sh without any subcommands. Click seems to support this with click-default-group.

I'm not sure it's wise using a dependence with only 50 stars and hasn't been touched for 4 years

@Pwuts
Copy link
Member

Pwuts commented Apr 18, 2023

It has good test coverage, is used by 3.7k projects/people, and we could pin it to the current version if we want to be certain it keeps working. There are also ways to achieve the same in bash, but the lib seems like a cleaner option.

@Swiftyos
Copy link
Contributor Author

@Pwuts Figured out how to do it without adding another dependency:

➜  Auto-GPT git:(click-arg-pasing) ✗ ./run.sh 
All packages are installed.
Welcome to Auto-GPT!  run with '--help' for more information.
Create an AI-Assistant:  Enter the name of your AI and its role below. Entering nothing will load defaults.
Name your AI:  For example, 'Entrepreneur-GPT'
AI Name: 

help:

➜  Auto-GPT git:(click-arg-pasing) ✗ ./run.sh --help
All packages are installed.
Usage: python -m autogpt [OPTIONS] COMMAND [ARGS]...

  Welcome to AutoGPT an experimental open-source application showcasing the
  capabilities of the GPT-4 pushing the boundaries of AI.

  Start an Auto-GPT assistant.

Options:
  -c, --continuous                Enable Continuous Mode
  -y, --skip-reprompt             Skips the re-prompting messages at the
                                  beginning of the script
  -C, --ai-settings TEXT          Specifies which ai_settings.yaml file to
                                  use, will also automatically skip the re-
                                  prompt.
  -l, --continuous-limit INTEGER  Defines the number of times to run in
                                  continuous mode
  --speak                         Enable Speak Mode
  --debug                         Enable Debug Mode
  --gpt3only                      Enable GPT3.5 Only Mode
  --gpt4only                      Enable GPT4 Only Mode
  -m, --use-memory TEXT           Defines which Memory backend to use
  -b, --browser-name TEXT         Specifies which web-browser to use when
                                  using selenium to scrape the web.
  --allow-downloads               Dangerous: Allows Auto-GPT to download files
                                  natively.
  --help                          Show this message and exit.

Commands:
  test  An example sub command

Here is some sample code showing how a subcommand can be added:

@main.command()
def test() -> None:
    """An example sub command"""
    print("test")

Running with test passed as the subcommand:

➜  Auto-GPT git:(click-arg-pasing) ✗ ./run.sh test --help
All packages are installed.
Usage: python -m autogpt test [OPTIONS]

  An example sub command

Options:
  --help  Show this message and exit.

@Swiftyos Swiftyos requested a review from Pwuts April 18, 2023 13:02
@jayceslesar
Copy link
Contributor

click is also good because it handles some cases that argparse implied such as list arguments which is the biggest PITA with argparse (there is some nasty implied ordering in argparse)...

If you wanted to add some tests could do something like

import click
from click.testing import CliRunner

def test_cli():
    runner = CliRunner()
    result = runner.invoke(path.to.cli, ["my-cli-command", "my-click-options", "my-click-args"])
    assert result.exit_code == 0

@Swiftyos Swiftyos dismissed Pwuts’s stale review April 18, 2023 14:01

Made requested change

@Swiftyos Swiftyos merged commit fd4a2ed into Significant-Gravitas:master Apr 18, 2023
)
agent.start_interaction_loop()
"""
Welcome to AutoGPT an experimental open-source application showcasing the capabilities of the GPT-4 pushing the boundaries of AI.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

punctuation?

sindlinger pushed a commit to Orgsindlinger/Auto-GPT-WebUI that referenced this pull request Sep 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants