{"id":13747504,"url":"https://github.com/sshaw/optout","last_synced_at":"2025-08-16T16:33:15.019Z","repository":{"id":56886972,"uuid":"2641186","full_name":"sshaw/optout","owner":"sshaw","description":"The opposite of getopt(): validate an option hash and turn it into something appropriate for exec() and system()-like functions","archived":false,"fork":false,"pushed_at":"2019-04-24T20:57:22.000Z","size":39,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-05-11T17:03:15.912Z","etag":null,"topics":["command-line","exec","option-parser","ruby","shell"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sshaw.png","metadata":{"files":{"readme":"README.rdoc","changelog":"Changes","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2011-10-25T04:28:12.000Z","updated_at":"2023-04-29T20:02:08.000Z","dependencies_parsed_at":"2022-08-20T15:20:51.571Z","dependency_job_id":null,"html_url":"https://github.com/sshaw/optout","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sshaw%2Foptout","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sshaw%2Foptout/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sshaw%2Foptout/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sshaw%2Foptout/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sshaw","download_url":"https://codeload.github.com/sshaw/optout/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":229805742,"owners_count":18126902,"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":["command-line","exec","option-parser","ruby","shell"],"created_at":"2024-08-03T06:01:31.595Z","updated_at":"2024-12-17T00:48:18.339Z","avatar_url":"https://github.com/sshaw.png","language":"Ruby","funding_links":[],"categories":["Ruby"],"sub_categories":[],"readme":"= Optout\n\n{\u003cimg src=\"https://secure.travis-ci.org/sshaw/optout.svg\"/\u003e}[http://travis-ci.org/sshaw/optout]\n\nOptout helps you write code that will call +exec+ and +system+ like functions. It allows you to map hash keys to command line\narguments and define validation rules that must be met before the command line arguments are created.\n\n== Overview\n\n require \"optout\"\n\n # Create options for `gem`\n optout = Optout.options do\n   on :gem, \"install\", :required =\u003e true\n   on :os, \"--platform\", %w(mswin cygwin mingw)\n   on :version, \"-v\", /\\A\\d+(\\.\\d+)*\\z/\n   on :user, \"--user-install\"\n   on :location, \"-i\", Optout::Dir.exists.under(ENV[\"HOME\"])\n end\n\n options = {\n   :gem =\u003e \"rake\",\n   :os =\u003e \"mswin\",\n   :version =\u003e \"0.9.2\",\n   :user =\u003e true\n }\n\n exec \"gem\", *optout.argv(options)\n # Returns: [\"install\", \"rake\", \"--platform\", \"mswin\", \"-v\", \"0.9.2\", \"--user-install\"]\n\n `gem #{optout.shell(options)}`\n # Returns: \"'install' 'rake' --platform 'mswin' -v '0.9.2' --user-install\"\n\n== Install\n\n\u003ccode\u003egem install optout\u003c/code\u003e\n\n== Defining Options\n\nInorder to turn the incoming option hash into something useful you must tell +Optout+ a bit about your options. This is done by calling \u003ccode\u003eOptout#on\u003c/code\u003e and passing it the name of a key in the option hash. The simplest case is an option with no switch:\n\n  optout = Optout.options do\n    on :path\n  end\n\n  optout.shell(:path =\u003e \"/home/sshaw\")\n  # Returns: '/home/sshaw'\n\nKey names can be a +Symbol+ or a +String+, +Optout+ will check for both in the option hash.\n\nIf the option has a switch it can be given after the option's key:\n\n  optout = Optout.options do\n    on :path, \"-p\"\n  end\n\n  optout.shell(:path =\u003e \"/home/sshaw\")\n  # Returns: -p '/home/sshaw'\n\nSome programs can be finicky about the space between the switch and the value, or require options\nin a different format. +Optout+ accepts various configuration options that can remdy this:\n\n  optout = Optout.options do\n    on :path, \"-p\", :arg_separator =\u003e \"\"\n  end\n\n  optout.shell(:path =\u003e \"/home/sshaw\")\n  # Returns: -p'/home/sshaw'\n\n  optout = Optout.options do\n    on :path, \"--path\", :arg_separator =\u003e \"=\", :required =\u003e true\n  end\n\n  optout.shell(:path =\u003e \"/home/sshaw\")\n  # Returns: --path='/home/sshaw'\n\n  optout.shell({})\n  # Raises: Optout::OptionRequired\n\nOptions can be grouped into required and optional:\n\n  Optout.options :arg_separator =\u003e \"=\" do\n    required do\n      on :in, \"if\"\n      on :out, \"of\"\n    end\n\n    optional do\n      on :size, \"size\"\n      on :count, \"count\"\n    end\n  end\n\n  optout.shell(:in =\u003e \"/dev/zero\", :out =\u003e \"/var/log/secure\")\n  # Returns: in='/dev/zero' out='/var/log/secure'\n\n== Validating Options\n\n+Optout+ can validate your options too. Just specify the validation rule after the option's key or switch:\n\n  optout = Optout.options do\n    # Must match [a-z]\n    on :path, \"-p\", /[a-z]/\n  end\n\n  optout = Optout.options do\n    # Must be true, false, or nil (add :required =\u003e true to allow only true or false)\n    on :path, \"-p\", Optout::Boolean\n  end\n\n  optout = Optout.options do\n    # Must be in the given set\n    on :path, %w(/home/sshaw /Users/gatinha /Users/fofinha)\n  end\n\n  optout = Optout.options do\n    # Must be a diretory under \"/home\" and have user read and write permissions\n    on :path, Optout::Dir.under(\"/home\").permissions(\"rw\")\n  end\n\n  optout.shell(:path =\u003e \"/root\")\n  # Raises: Optout::OptionInvalid\n\n== TODOs\n\n* Proper \u003ccode\u003ecmd.exe\u003c/code\u003e quoting\n* Mutually exclusive options\n* Split options i.e., \u003ccode\u003e:jvm =\u003e %w[A B C]\u003c/code\u003e would be created as \u003ccode\u003e-XA -XB -XC\u003c/code\u003e\n* Validate based on the presence of other options\n\n== More Info\n\n* {RDoc}[https://rdoc.info/gems/optout]\n* {Bugs}[https://github.com/sshaw/optout/issues]\n\n== Author\n\nSkye Shaw [skye.shaw AT gmail]\n\n== License\n\nCopyright (c) 2011-2019 Skye Shaw\n\nReleased under the MIT License: http://www.opensource.org/licenses/MIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsshaw%2Foptout","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsshaw%2Foptout","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsshaw%2Foptout/lists"}