{"id":16104743,"url":"https://github.com/sergeymakinen/go-cmdbuilder","last_synced_at":"2025-04-06T02:28:39.146Z","repository":{"id":57511188,"uuid":"224252147","full_name":"sergeymakinen/go-cmdbuilder","owner":"sergeymakinen","description":"Converter from structs defining command-line arguments and their values back to a command-line","archived":false,"fork":false,"pushed_at":"2024-02-23T10:25:47.000Z","size":60,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-02T04:37:06.960Z","etag":null,"topics":["arguments-converter","cmd","cmdline","command-line","commandline","flag","flags","go","golang"],"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/sergeymakinen.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-11-26T17:46:04.000Z","updated_at":"2024-02-23T10:22:27.000Z","dependencies_parsed_at":"2024-06-20T09:20:49.654Z","dependency_job_id":"0ff2d5a4-d938-4fed-bc8c-730f16194ef1","html_url":"https://github.com/sergeymakinen/go-cmdbuilder","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sergeymakinen%2Fgo-cmdbuilder","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sergeymakinen%2Fgo-cmdbuilder/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sergeymakinen%2Fgo-cmdbuilder/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sergeymakinen%2Fgo-cmdbuilder/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sergeymakinen","download_url":"https://codeload.github.com/sergeymakinen/go-cmdbuilder/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247425507,"owners_count":20936966,"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":["arguments-converter","cmd","cmdline","command-line","commandline","flag","flags","go","golang"],"created_at":"2024-10-09T19:06:07.424Z","updated_at":"2025-04-06T02:28:39.130Z","avatar_url":"https://github.com/sergeymakinen.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# cmdbuilder\n\n[![tests](https://github.com/sergeymakinen/go-cmdbuilder/workflows/tests/badge.svg)](https://github.com/sergeymakinen/go-cmdbuilder/actions?query=workflow%3Atests)\n[![Go Reference](https://pkg.go.dev/badge/github.com/sergeymakinen/go-cmdbuilder.svg)](https://pkg.go.dev/github.com/sergeymakinen/go-cmdbuilder/v2)\n[![Go Report Card](https://goreportcard.com/badge/github.com/sergeymakinen/go-cmdbuilder)](https://goreportcard.com/report/github.com/sergeymakinen/go-cmdbuilder)\n[![codecov](https://codecov.io/gh/sergeymakinen/go-cmdbuilder/branch/main/graph/badge.svg)](https://codecov.io/gh/sergeymakinen/go-cmdbuilder)\n\nPackage cmdbuilder implements a converter from structs defining command-line arguments and their values back to a command-line.\n\nThe cmdbuilder package aims to be compatible with the flags package (see https://github.com/jessevdk/go-flags),\nso it also uses structs, the reflection and struct field tags to specify command-line arguments. For example:\n\n```go\ntype Options struct {\n    Verbose    []bool            `short:\"v\" long:\"verbose\"`\n    AuthorInfo map[string]string `short:\"a\"`\n    Name       string            `long:\"name\" optional:\"true\"`\n}\n```\n\nThis specifies the `Verbose` boolean option with a short name `-v` and a long name `--verbose`,\nthe `AuthorInfo` map option with a short name `-a`,\nand the `Name` string option with a long name `--name` and an optional value.\nIf the struct is initialized as the following:\n\n```go\nopts := Options{\n    Verbose:    []bool{true, true, true},\n    AuthorInfo: map[string]string{\"name\": \"Jesse\", \"surname\": \"van den Kieboom\"},\n    Name:       \"Sergey Makinen\",\n}\n```\n\nThen the `CommandLine` function will produce the following string:\n\n```\n-vvv -a name:Jesse -a \"surname:van den Kieboom\" --name=\"Sergey Makinen\"\n```\n\nAny type that implements the `Marshaler` interface may fully customize its value output.\n\n## Installation\n\nUse go get:\n\n```bash\ngo get github.com/sergeymakinen/go-cmdbuilder/v2\n```\n\nThen import the package into your own code:\n\n```go\nimport \"github.com/sergeymakinen/go-cmdbuilder/v2\"\n```\n\n\n## Example\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/jessevdk/go-flags\"\n\t\"github.com/sergeymakinen/go-cmdbuilder/v2\"\n)\n\ntype Options struct {\n\t// Slice of bool will append 'true' each time the option\n\t// is encountered (can be set multiple times, like -vvv)\n\tVerbose []bool `short:\"v\" long:\"verbose\" description:\"Show verbose debug information\"`\n\n\t// Example of automatic marshalling to desired type (uint)\n\tOffset uint `long:\"offset\" description:\"Offset\"`\n\n\t// Example of a required flag\n\tName string `short:\"n\" long:\"name\" description:\"A name\" required:\"true\"`\n\n\t// Example of a flag restricted to a pre-defined set of strings\n\tAnimal string `long:\"animal\" choice:\"cat\" choice:\"dog\"`\n\n\t// Example of a value name\n\tFile string `short:\"f\" long:\"file\" description:\"A file\" value-name:\"FILE\"`\n\n\t// Example of a pointer\n\tPtr *int `short:\"p\" description:\"A pointer to an integer\"`\n\n\t// Example of a slice of strings\n\tStringSlice []string `short:\"s\" description:\"A slice of strings\"`\n\n\t// Example of a slice of pointers\n\tPtrSlice []*string `long:\"ptrslice\" description:\"A slice of pointers to string\"`\n\n\t// Example of a map\n\tIntMap map[string]int `long:\"intmap\" description:\"A map from string to int\"`\n\n\t// Example of positional arguments\n\tPositional struct {\n\t\tRest []string\n\t} `positional-args:\"yes\"`\n}\n\n// main demonstrates the usage of CommandLine using an example struct\n// similar to the flags package example.\nfunc main() {\n\tvar opts Options\n\targs := []string{\n\t\t\"-vv\",\n\t\t\"--offset=5\",\n\t\t\"-n\", \"Me\",\n\t\t\"--animal\", \"dog\",\n\t\t\"-p\", \"3\",\n\t\t\"-s\", \"hello\",\n\t\t\"-s\", \"world\",\n\t\t\"--ptrslice\", \"hello\",\n\t\t\"--ptrslice\", \"world\",\n\t\t\"--intmap\", \"a:1\",\n\t\t\"--intmap\", \"b:5\",\n\t\t\"arg1\",\n\t\t\"arg2\",\n\t\t\"arg3\",\n\t}\n\t_, err := flags.ParseArgs(\u0026opts, args)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Printf(\"Verbose: %+v\\nOffset: %+v\\nName: %+v\\nAnimal: %+v\\nPtr: %+v\\nStringSlice: %+v\\nPtrSlice: [%v %v]\\nIntMap: %+v\\nPositional: %+v\\n\\n\", opts.Verbose, opts.Offset, opts.Name, opts.Animal, *opts.Ptr, opts.StringSlice, *opts.PtrSlice[0], *opts.PtrSlice[1], opts.IntMap, opts.Positional)\n\tcmd, _ := cmdbuilder.CommandLine(opts)\n\tfmt.Printf(\"Command line: %s\\n\", cmd)\n\t// Output:\n\t// Verbose: [true true]\n\t// Offset: 5\n\t// Name: Me\n\t// Animal: dog\n\t// Ptr: 3\n\t// StringSlice: [hello world]\n\t// PtrSlice: [hello world]\n\t// IntMap: map[a:1 b:5]\n\t// Positional: {Rest:[arg1 arg2 arg3]}\n\t//\n\t// Command line: -vv --offset 5 -n Me --animal dog -p 3 -s hello -s world --ptrslice hello --ptrslice world --intmap a:1 --intmap b:5 arg1 arg2 arg3\n}\n```\n\n## License\n\nBSD 3-Clause\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsergeymakinen%2Fgo-cmdbuilder","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsergeymakinen%2Fgo-cmdbuilder","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsergeymakinen%2Fgo-cmdbuilder/lists"}