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

Implement argparse.prog style #55

Closed
kotfu opened this issue Feb 18, 2023 · 5 comments · Fixed by #56
Closed

Implement argparse.prog style #55

kotfu opened this issue Feb 18, 2023 · 5 comments · Fixed by #56
Labels
argparse Concerns argparse enhancement New feature or request

Comments

@kotfu
Copy link

kotfu commented Feb 18, 2023

Add a new style to highlight the name of the program. This might look like

RichHelpFormatter.styles["argparse.prog"] = "deep_sky_blue1"

This style would be applied to the prog parameter of the argparse.ArgumentParser() constructor. In the example below, the prog is connect:

prsr = argparse.ArgumentParser(
    prog="connect",
    description="Edit or show the location of the user configuration file.",
    usage="%(prog)s [-h] config_name\n       %(prog)s [-h] url [user] [password]",
    formatter_class=rich_argparse.RichHelpFormatter,
)

This formatting should be applied whether the usage parameter is given, as it is in the example above, or if the usage parameter is absent.

@hamdanal hamdanal added the enhancement New feature or request label Feb 18, 2023
@hamdanal
Copy link
Owner

This is not as simple as it looks as argparse calls formatter.format_help() internally to get the prog parameter when creating subparsers. Adding styles to prog may mess up the subparsers use case. I'll look into it more to see if a solution can be implemented that doesn't break subparsers.

@hamdanal
Copy link
Owner

This is implemented in #56.
@kotfu I appreciate it if you could take a look at that PR and tell me if it answers your request.

hamdanal added a commit that referenced this issue Feb 25, 2023
Closes #55

Add a new style for `%(prog)s` in the usage. The style is applied in
argparse-generated usage and in user defined usage whether the user
usage is plain text or rich markup.
@kotfu
Copy link
Author

kotfu commented Feb 27, 2023

This works exactly as I hoped. Thanks for implementing it. 🥳

@kotfu
Copy link
Author

kotfu commented Feb 27, 2023

After further experimenting, I think I found a bug which was introduced by this PR. Consider the following:

import argparse
import rich.console
from rich_argparse import RichHelpFormatter

console = rich.console.Console(markup=False, emoji=False, highlight=False)
parser = argparse.ArgumentParser(
    prog="list",
    description="Show all installed applications",
    add_help=False,
    formatter_class=RichHelpFormatter,
)
parser.add_argument(
    "config_name",
    nargs="?",
    help="a configuration name",
)
parser.add_argument(
    "-r",
    "--raw",
    action="store_true",
    required=True,
    help="show apps without formatting",
)
parser.add_argument(
    "-s",
    "--state",
    choices=["running", "stopped"],
    help="only show apps in a given state",
)
console.print(parser.format_help())

In the released 1.0.0 version, the output is:

Usage: list -r [-s {running,stopped}] [config_name]

Show all installed applications

Positional Arguments:
  config_name           a configuration name

Options:
  -r, --raw             show apps without formatting
  -s, --state {running,stopped}
                        only show apps in a given state

In the unreleased main branch, the output is:

Usage: list -r [-s {running,stopped}]
[config_name]

Show all installed applications

Positional Arguments:
  config_name           a configuration name

Options:
  -r, --raw             show apps without formatting
  -s, --state {running,stopped}
                        only show apps in a given state

@hamdanal
Copy link
Owner

After further experimenting, I think I found a bug which was introduced by this PR

Thanks for the report. Could you please open a new issue for this? I'll take a look soon.

@hamdanal hamdanal added the argparse Concerns argparse label Aug 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
argparse Concerns argparse enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants