{"id":15649751,"url":"https://github.com/ioquatix/samovar","last_synced_at":"2025-06-20T02:34:49.245Z","repository":{"id":56894217,"uuid":"56853422","full_name":"ioquatix/samovar","owner":"ioquatix","description":null,"archived":false,"fork":false,"pushed_at":"2024-03-28T12:54:24.000Z","size":210,"stargazers_count":39,"open_issues_count":3,"forks_count":3,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-06-01T23:57:38.915Z","etag":null,"topics":["documentation-generator","nested-commands","option-parser","parsing","ruby"],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ioquatix.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license.md","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":"2016-04-22T12:21:54.000Z","updated_at":"2025-03-16T20:16:57.000Z","dependencies_parsed_at":"2024-03-28T13:59:17.443Z","dependency_job_id":"70ef096a-d1de-4a43-b3a6-b579806158ca","html_url":"https://github.com/ioquatix/samovar","commit_stats":null,"previous_names":[],"tags_count":25,"template":false,"template_full_name":null,"purl":"pkg:github/ioquatix/samovar","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ioquatix%2Fsamovar","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ioquatix%2Fsamovar/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ioquatix%2Fsamovar/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ioquatix%2Fsamovar/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ioquatix","download_url":"https://codeload.github.com/ioquatix/samovar/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ioquatix%2Fsamovar/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260866622,"owners_count":23074856,"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":["documentation-generator","nested-commands","option-parser","parsing","ruby"],"created_at":"2024-10-03T12:31:43.222Z","updated_at":"2025-06-20T02:34:44.231Z","avatar_url":"https://github.com/ioquatix.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Samovar\n\n![Teapot](teapot.png)\n\nSamovar is a modern framework for building command-line tools and applications. It provides a declarative class-based DSL for building command-line parsers that include automatic documentation generation. It helps you keep your functionality clean and isolated where possible.\n\n[![Development Status](https://github.com/ioquatix/samovar/workflows/Test/badge.svg)](https://github.com/ioquatix/samovar/actions?workflow=Test)\n\n## Motivation\n\nI've been using [Optimist](https://github.com/ManageIQ/optimist) and while it's not bad, it's hard to use for sub-commands in a way that generates nice documentation. It also has pretty limited support for complex command lines (e.g. nested commands, splits, matching tokens, etc). Samovar is a high level bridge between the command line and your code: it generates decent documentation, maps nicely between the command line syntax and your functions, and supports sub-commands using classes which are easy to compose.\n\nOne of the other issues I had with existing frameworks is testability. Most frameworks expect to have some pretty heavy logic directly in the binary executable, or at least don't structure your code in a way which makes testing easy. Samovar structures your command processing logic into classes which can be easily tested in isolation, which means that you can mock up and [spec your command-line executables easily](https://github.com/ioquatix/teapot/blob/master/spec/teapot/command_spec.rb).\n\n## Examples\n\n  - [Teapot](https://github.com/ioquatix/teapot/blob/master/lib/teapot/command.rb) is a build system and uses multiple top-level commands.\n  - [Utopia](https://github.com/ioquatix/utopia/blob/master/lib/utopia/command.rb) is a web application platform and uses nested commands.\n  - [Synco](https://github.com/ioquatix/synco/blob/master/lib/synco/command.rb) is a backup tool and sends commands across the network and has lots of options with default values.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n    gem 'samovar'\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install samovar\n\n## Usage\n\nGenerally speaking, you should create `Command` classes that represent specific functions in your program. The top level command might look something like this:\n\n``` ruby\nrequire 'samovar'\n\nclass List \u003c Samovar::Command\n\tself.description = \"List the current directory\"\n\t\n\tdef call\n\t\tsystem(\"ls -lah\")\n\tend\nend\n\nclass Application \u003c Samovar::Command\n\toptions do\n\t\toption '--help', \"Do you need help?\"\n\tend\n\t\n\tnested :command, {\n\t\t'list' =\u003e List\n\t}, default: 'list'\n\t\n\tdef call\n\t\tif @options[:help]\n\t\t\tself.print_usage\n\t\telse\n\t\t\t@command.call\n\t\tend\n\tend\nend\n\nApplication.call # Defaults to ARGV.\n```\n\n### Basic Options\n\n``` ruby\nrequire 'samovar'\n\nclass Application \u003c Samovar::Command\n\toptions do\n\t\toption '-f/--frobulate \u003ctext\u003e', \"Frobulate the text\"\n\t\toption '-x | -y', \"Specify either x or y axis.\", key: :axis\n\t\toption '-F/--yeah/--flag', \"A boolean flag with several forms.\"\n\t\toption '--things \u003ca,b,c\u003e', \"A list of things\" do |value|\n\t\t\tvalue.split(/\\s*,\\s*/)\n\t\tend\n\tend\nend\n\napplication = Application.new(['-f', 'Algebraic!'])\napplication.options[:frobulate] # 'Algebraic!'\n\napplication = Application.new(['-x', '-y'])\napplication.options[:axis] # :y\n\napplication = Application.new(['-F'])\napplication.options[:flag] # true\n\napplication = Application.new(['--things', 'x,y,z'])\napplication.options[:things] # ['x', 'y', 'z']\n```\n\n### Nested Commands\n\n``` ruby\nrequire 'samovar'\n\nclass Create \u003c Samovar::Command\n\tdef invoke(parent)\n\t\tputs \"Creating\"\n\tend\nend\n\nclass Application \u003c Samovar::Command\n\tnested '\u003ccommand\u003e',\n\t\t'create' =\u003e Create\n\t\n\tdef invoke(program_name: File.basename($0))\n\t\tif @command\n\t\t\t@command.invoke\n\t\telse\n\t\t\tprint_usage(program_name)\n\t\tend\n\tend\nend\n\nApplication.new(['create']).invoke\n```\n\n### ARGV Splits\n\n``` ruby\nrequire 'samovar'\n\nclass Application \u003c Samovar::Command\n\tmany :packages\n\tsplit :argv\nend\n\napplication = Application.new(['foo', 'bar', 'baz', '--', 'apples', 'oranges', 'feijoas'])\napplication.packages # ['foo', 'bar', 'baz']\napplication.argv # ['apples', 'oranges', 'feijoas']\n```\n\n### Parsing Tokens\n\n``` ruby\nrequire 'samovar'\n\nclass Application \u003c Samovar::Command\n\tself.description = \"Mix together your favorite things.\"\n\t\n\tone :fruit, \"Name one fruit\"\n\tmany :cakes, \"Any cakes you like\"\nend\n\napplication = Application.new(['apple', 'chocolate cake', 'fruit cake'])\napplication.fruit # 'apple'\napplication.cakes # ['chocolate cake', 'fruit cake']\n```\n\n### Explicit Commands\n\nGiven a custom `Samovar::Command` subclass, you can instantiate it with options:\n\n``` ruby\napplication = Application['--root', path]\n```\n\nYou can also duplicate an existing command instance with additions/changes:\n\n``` ruby\nconcurrent_application = application['--threads', 12]\n```\n\nThese forms can be useful when invoking one command from another, or in unit tests.\n\n## Contributing\n\nWe welcome contributions to this project.\n\n1.  Fork it.\n2.  Create your feature branch (`git checkout -b my-new-feature`).\n3.  Commit your changes (`git commit -am 'Add some feature'`).\n4.  Push to the branch (`git push origin my-new-feature`).\n5.  Create new Pull Request.\n\n### Developer Certificate of Origin\n\nThis project uses the [Developer Certificate of Origin](https://developercertificate.org/). All contributors to this project must agree to this document to have their contributions accepted.\n\n### Contributor Covenant\n\nThis project is governed by the [Contributor Covenant](https://www.contributor-covenant.org/). All contributors and participants agree to abide by its terms.\n\n## Future Work\n\n### Multi-value Options\n\nRight now, options can take a single argument, e.g. `--count \u003cint\u003e`. Ideally, we support a specific sub-parser defined by the option, e.g. `--count \u003cint...\u003e` or `--tag \u003csection\u003e \u003ctags...\u003e`. These would map to specific parsers using `Samovar::One` and `Samovar::Many` internally.\n\n### Global Options\n\nOptions can only be parsed at the place they are explicitly mentioned, e.g. a command with sub-commands won't parse an option added to the end of the command:\n\n``` ruby\ncommand list --help\n```\n\nOne might reasonably expect this to parse but it isn't so easy to generalize this:\n\n``` ruby\ncommand list -- --help\n```\n\nIn this case, do we show help? Some effort is required to disambiguate this. Initially, it makes sense to keep things as simple as possible. But, it might make sense for some options to be declared in a global scope, which are extracted before parsing begins. I'm not sure if this is really a good idea. It might just be better to give good error output in this case (you specified an option but it was in the wrong place).\n\n### Shell Auto-completion\n\nBecause of the structure of the Samovar command parser, it should be possible to generate a list of all possible tokens at each point. Therefore, semantically correct tab completion should be possible.\n\nAs a secondary to this, it would be nice if `Samovar::One` and `Samovar::Many` could take a list of potential tokens so that auto-completion could give meaningful suggestions, and possibly improved validation.\n\n### Short/Long Help\n\nIt might be interesting to explore whether it's possible to have `-h` and `--help` do different things. This could include command specific help output, more detailed help output (similar to a man page), and other useful help related tasks.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fioquatix%2Fsamovar","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fioquatix%2Fsamovar","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fioquatix%2Fsamovar/lists"}