Skip to content

Option to display metavar for non-toggle flags #105

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

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