Skip to content

Option to display metavar for non-toggle flags #105

Open

Description

@unrealtournament

Hello,

I've been using flaggy in different projects for a while and it has been great, but there is one issue that bothers me, there's no metavar display for flags that require a value, which can be confusing and make flags seem like they don't require a value when they do, or vice versa, if the flag names aren't super obvious. Most other popular argument parsing libraries display this information.

A simple example of flaggy's current behavior:

package main

import "github.com/integrii/flaggy"

func main() {
	var strVar string
	var boolVar bool

	flaggy.String(&strVar, "c", "config", "Config file")
	flaggy.Bool(&boolVar, "v", "verbose", "Verbose output")
	flaggy.DisableCompletion()

	flaggy.Parse()
}

This prints:

argtest

  Flags:
        --version   Displays the program version string.
    -h  --help      Displays help with available flag, subcommand, and positional value parameters.
    -c  --config    Config file
    -v  --verbose   Verbose output

Same example with Python's argparse:

import argparse

parser = argparse.ArgumentParser(description="argtest")
parser.add_argument("-c", "--config", help="Config file")
parser.add_argument("--config2", help="Config file with custom metavar", metavar="cfg-file")
parser.add_argument("-v", "--verbose", help="Verbose output", action="store_true")

args = parser.parse_args()

Prints:

usage: main.py [-h] [-c CONFIG] [--config2 cfg-file] [-v]

argtest

options:
  -h, --help            show this help message and exit
  -c CONFIG, --config CONFIG
                        Config file
  --config2 cfg-file    Config file with custom metavar
  -v, --verbose         Verbose output

Linux coreutils also follow a similar format (excerpt from grep --help):

  -e, --regexp=PATTERNS     use PATTERNS for matching
  -f, --file=FILE           take PATTERNS from FILE
  -i, --ignore-case         ignore case distinctions in patterns and data

It would be great if there was an option to configure whether to display the metavar or not (in case people want to preserve the current behavior), and to change the default metavar name.

Activity

  1. integrii commented on Jul 28, 2026

    @integrii
    Owner

    Thanks for your appreciation of the project!

    Just thinking about this, I haven't been able to figure out what the best way to formulate the metavar string. Things that have come to mind so far:

    • The long flag name if it exists
    • The short flag name if it exists
    • The variable name inside the code, perhaps capitalized
    • Some new input (I dislike this because it means adding new properties to existing or new flag inputs funcs)

    Do you have any ideas what you would like to see there? If this can be done simply, then it's relatively easy to add it as a global toggle for the package to enable.

    Also, your username @unrealtournament is perhaps serendipity, but I do work Epic Games. If you're someone who works there, say "hi" on Slack!

  2. integrii commented on Jul 28, 2026

    @integrii
    Owner

    I opened draft PR #106 with an opt-in implementation.

    It derives metavars from flag names (--config-file → CONFIG_FILE, -p → P), leaves boolean toggles unchanged, and keeps metavars disabled by default. Enable metavars with flaggy.EnableMetavars() or p.EnableMetavars().

  3. unrealtournament commented on Jul 28, 2026

    @unrealtournament
    Author

    I think your proposed solution is good, without having to break current function signatures by adding additional parameters. I would be ok with that.

    The ideal situation would be to be able to specify the metavar name, but due to how flaggy is structured it would complicate things, and its a rather niche thing to have to change the metavar anyway, so I don't think it's worth it. Other libraries, such as pflag just add the type (--config string), but I prefer it the way you have chosen to do it, as its a bit more expressive.

    P.S: Unfortunately the username is just coincidence, I just like the game :)

  4. unrealtournament commented on Sep 12, 2026

    @unrealtournament
    Author

    Looks like the PR is pretty much ready and working, could this be merged (and released)? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions