{"id":16913688,"url":"https://github.com/pborman/getopt","last_synced_at":"2025-04-04T14:09:01.892Z","repository":{"id":33189722,"uuid":"36831672","full_name":"pborman/getopt","owner":"pborman","description":"getopt style option parsing for Go","archived":false,"fork":false,"pushed_at":"2022-07-10T17:15:53.000Z","size":102,"stargazers_count":120,"open_issues_count":8,"forks_count":18,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-10-14T19:14:24.665Z","etag":null,"topics":["getopt","go"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pborman.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-06-03T21:30:04.000Z","updated_at":"2024-05-28T07:48:11.000Z","dependencies_parsed_at":"2022-08-17T20:10:53.152Z","dependency_job_id":null,"html_url":"https://github.com/pborman/getopt","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pborman%2Fgetopt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pborman%2Fgetopt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pborman%2Fgetopt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pborman%2Fgetopt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pborman","download_url":"https://codeload.github.com/pborman/getopt/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247190252,"owners_count":20898702,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["getopt","go"],"created_at":"2024-10-13T19:14:43.652Z","updated_at":"2025-04-04T14:09:01.873Z","avatar_url":"https://github.com/pborman.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# getopt ![build status](https://travis-ci.org/pborman/getopt.svg?branch=master)\n\nPackage getopt provides traditional getopt processing for implementing\ncommands that use traditional command lines.  The standard Go flag package\ncannot be used to write a program that parses flags the way ls or ssh does,\nfor example.  There are two versions, v1 and v2, both named getopt, that\nuse the following import paths:\n\n```\n\t\"github.com/pborman/getopt\"     // version 1\n\t\"github.com/pborman/getopt/v2\"  // version 2\n```\n\nThis README describes version 2 of the package, which has a simplified API.\n\n## Usage\n\nGetopt supports functionality found in both the standard BSD getopt as well\nas (one of the many versions of) the GNU getopt_long.  Being a Go package,\nthis package makes common usage easy, but still enables more controlled usage\nif needed.\n\nTypical usage:\n\n```\n\tDeclare flags and have getopt return pointers to the values.\n\thelpFlag := getopt.Bool('?', \"display help\")\n\tcmdFlag := getopt.StringLong(\"command\", 'c', \"default\", \"the command\")\n\n\tDeclare flags against existing variables.\n\tvar (\n\t\tfileName = \"/the/default/path\"\n\t\ttimeout = time.Second * 5\n\t\tverbose bool\n\t)\n\tfunc init() {\n\t\tgetopt.Flag(\u0026verbose, 'v', \"be verbose\")\n\t\tgetopt.FlagLong(\u0026fileName, \"path\", 0, \"the path\")\n\t\tgetopt.FlagLong(\u0026timeout, \"timeout\", 't', \"some timeout\")\n\t}\n\n\tfunc main() {\n\t\tParse the program arguments\n\t\tgetopt.Parse()\n\t\tGet the remaining positional parameters\n\t\targs := getopt.Args()\n\t\t...\n```\n\nIf you don't want the program to exit on error, use getopt.Getopt:\n\n```\n\t\terr := getopt.Getopt(nil)\n\t\tif err != nil {\n\t\t\tcode to handle error\n\t\t\tfmt.Fprintln(os.Stderr, err)\n\t\t}\n```\n\n## Flag Syntax\n\nSupport is provided for both short (-f) and long (--flag) options.  A single\noption may have both a short and a long name.  Each option may be a flag or a\nvalue.  A value takes an argument.\n\nDeclaring no long names causes this package to process arguments like the\ntraditional BSD getopt.\n\nShort flags may be combined into a single parameter.  For example, \"-a -b -c\"\nmay also be expressed \"-abc\".  Long flags must stand on their own \"--alpha\n--beta\"\n\nValues require an argument.  For short options the argument may either be\nimmediately following the short name or as the next argument.  Only one short\nvalue may be combined with short flags in a single argument; the short value\nmust be after all short flags.  For example, if f is a flag and v is a value,\nthen:\n\n```\n\t-vvalue    (sets v to \"value\")\n\t-v value   (sets v to \"value\")\n\t-fvvalue   (sets f, and sets v to \"value\")\n\t-fv value  (sets f, and sets v to \"value\")\n\t-vf value  (set v to \"f\" and value is the first parameter)\n```\n\nFor the long value option val:\n\n```\n\t--val value (sets val to \"value\")\n\t--val=value (sets val to \"value\")\n\t--valvalue  (invalid option \"valvalue\")\n```\n\nValues with an optional value only set the value if the value is part of the\nsame argument.  In any event, the option count is increased and the option is\nmarked as seen.\n\n```\n\t-v -f          (sets v and f as being seen)\n\t-vvalue -f     (sets v to \"value\" and sets f)\n\t--val -f       (sets v and f as being seen)\n\t--val=value -f (sets v to \"value\" and sets f)\n```\n\nThere is no convience function defined for making the value optional.  The\nSetOptional method must be called on the actual Option.\n\n```\n\tv := String(\"val\", 'v', \"\", \"the optional v\")\n\tLookup(\"v\").SetOptional()\n\n\tvar s string\n\tFlagLong(\u0026s, \"val\", 'v', \"the optional v).SetOptional()\n```\n\nParsing continues until the first non-option or \"--\" is encountered.\n\nThe short name \"-\" can be used, but it either is specified as \"-\" or as part\nof a group of options, for example \"-f-\".  If there are no long options\nspecified then \"--f\" could also be used.  If \"-\" is not declared as an option\nthen the single \"-\" will also terminate the option processing but unlike\n\"--\", the \"-\" will be part of the remaining arguments.\n\n## Advanced Usage\n\nNormally the parsing is performed by calling the Parse function.  If it is\nimportant to see the order of the options then the Getopt function should be\nused.  The standard Parse function does the equivalent of:\n\n```\nfunc Parse() {\n\tif err := getopt.Getopt(os.Args, nil); err != nil {\n\t\tfmt.Fprintln(os.Stderr, err)\n\t\ts.usage()\n\t\tos.Exit(1)\n\t}\n}\n```\n\nWhen calling Getopt it is the responsibility of the caller to print any\nerrors.\n\nNormally the default option set, CommandLine, is used.  Other option sets may\nbe created with New.\n\nAfter parsing, the sets Args will contain the non-option arguments.  If an\nerror is encountered then Args will begin with argument that caused the\nerror.\n\nIt is valid to call a set's Parse a second time to amend the current set of\nflags or values.  As an example:\n\n```\n\tvar a = getopt.Bool('a', \"\", \"The a flag\")\n\tvar b = getopt.Bool('b', \"\", \"The a flag\")\n\tvar cmd = \"\"\n\n\tvar opts = getopt.CommandLine\n\n\topts.Parse(os.Args)\n\tif opts.NArgs() \u003e 0 {\n\t\tcmd = opts.Arg(0)\n\t\topts.Parse(opts.Args())\n\t}\n```\n\nIf called with set to { \"prog\", \"-a\", \"cmd\", \"-b\", \"arg\" } then both a and\nb would be set, cmd would be set to \"cmd\", and opts.Args() would return {\n\"arg\" }.\n\nUnless an option type explicitly prohibits it, an option may appear more than\nonce in the arguments.  The last value provided to the option is the value.\n\n## Builtin Types\n\nThe Flag and FlagLong functions support most standard Go types.  For the\nlist, see the description of FlagLong below for a list of supported types.\n\nThere are also helper routines to allow single line flag declarations.  These\ntypes are: Bool, Counter, Duration, Enum, Int16, Int32, Int64, Int, List,\nSigned, String, Uint16, Uint32, Uint64, Uint, and Unsigned.\n\nEach comes in a short and long flavor, e.g., Bool and BoolLong and include\nfunctions to set the flags on the standard command line or for a specific Set\nof flags.\n\nExcept for the Counter, Enum, Signed and Unsigned types, all of these types\ncan be declared using Flag and FlagLong by passing in a pointer to the\nappropriate type.\n\n## Declaring New Flag Types\n\nA pointer to any type that implements the Value interface may be passed to\nFlag or FlagLong.\n\n## VALUEHELP\n\nAll non-flag options are created with a \"valuehelp\" as the last parameter.\nValuehelp should be 0, 1, or 2 strings.  The first string, if provided, is\nthe usage message for the option.  If the second string, if provided, is the\nname to use for the value when displaying the usage.  If not provided the\nterm \"value\" is assumed.\n\nThe usage message for the option created with\n\n```\n\tStringLong(\"option\", 'o', \"defval\", \"a string of letters\")\n```\n\nis\n\n```\n\t-o, -option=value\n```\nwhile the usage message for the option created with\n\n```\n\tStringLong(\"option\", 'o', \"defval\", \"a string of letters\", \"string\")\n```\n\nis\n\n```\n\t-o, -option=string\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpborman%2Fgetopt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpborman%2Fgetopt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpborman%2Fgetopt/lists"}