{"id":15494904,"url":"https://github.com/fracpete/simple-argparse4j","last_synced_at":"2025-03-28T17:14:34.715Z","repository":{"id":57719812,"uuid":"115840071","full_name":"fracpete/simple-argparse4j","owner":"fracpete","description":"Very simple argument parser, inspired by argparse4j (https://argparse4j.github.io/).","archived":false,"fork":false,"pushed_at":"2020-10-13T07:47:02.000Z","size":95,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-19T12:15:48.090Z","etag":null,"topics":["java","optionparser-library"],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fracpete.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}},"created_at":"2017-12-31T03:28:38.000Z","updated_at":"2020-10-13T07:46:55.000Z","dependencies_parsed_at":"2022-09-13T12:50:27.580Z","dependency_job_id":null,"html_url":"https://github.com/fracpete/simple-argparse4j","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fracpete%2Fsimple-argparse4j","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fracpete%2Fsimple-argparse4j/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fracpete%2Fsimple-argparse4j/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fracpete%2Fsimple-argparse4j/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fracpete","download_url":"https://codeload.github.com/fracpete/simple-argparse4j/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246068301,"owners_count":20718503,"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":["java","optionparser-library"],"created_at":"2024-10-02T08:15:34.341Z","updated_at":"2025-03-28T17:14:34.697Z","avatar_url":"https://github.com/fracpete.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# simple-argparse4j\nVery simple argument parser, inspired by argparse4j (https://argparse4j.github.io/).\n\nNormally, I use *argparse4j* for all my little command-line based tools. \nBut for one project, I needed to be able to have unparsed options that resemble\noptions, as I needed to pass on these options to processes that get launched.\nI didn't find a solution to get *argparse4j* to be more lenient, hence I \nimplemented a very simple version of a command-line option parser inspired\nby *argparse4j*.\n\n\n## Supported data types\n\nThe parser supports the following data types:\n\n* string\n* boolean\n* byte\n* short\n* int\n* long\n* float\n* double\n* file (any file/dir, existing file/dir, non-existing file/dir)\n\nApart from *boolean* all data types require an argument. \n\n## Defining options\n\nHere is an example for defining an option:\n\n```java\nimport com.github.fracpete.simpleargparse4j.ArgumentParser;\n...\nArgumentParser parser = new ArgumentParser(getName());\nparser.addOption(\"--name\")\n  .dest(\"name\")\n  .help(\"the name of the environment\")\n  .required(true);\n```\n\nThe parser will look for an option that starts with a flag of `--name`\nand stores the associated argument that it will find (using the key specified\nwith `dest(...)`). `help(...)` specifies the help string. \n`required(true)` specifies that the user *has* to supply the argument (by default, \noptions are optional). \n\nIt is also possible to supply two flags, e.g., a short form and a long version:\n```java\nimport com.github.fracpete.simpleargparse4j.ArgumentParser;\n...\nArgumentParser parser = new ArgumentParser(getName());\nparser.addOption(\"-n\", \"--name\")\n  .dest(\"name\")\n  .help(\"the name of the environment\")\n  .required(true);\n``` \n\nOther settings for `ArgumentParser`:\n\n* `screenWidth(int)` -- the width of the screen, which gets used when printing\n  the help screen.\n* `breakChars(char[])` -- the characters that are used to break long help lines\n  into smaller ones to fit within the defined screen width.\n\nOther settings for `Option`:\n\n* `argument(boolean)` -- specifies whether the option is a flag (`false`) or \n  requires an argument (`true`). By setting the `type` of an option to `boolean`,\n  this gets automatically set to `false`. \n* `multiple(boolean)` -- specifies whether the option can occur multiple times\n* `setDefault(...)` -- sets the default value (if not a required option);\n  in conjunction with `multiple(true)`, you have to set a `java.util.List` object\n* `type(Type)` -- sets the type to enforce when parsing the options rather than \n  when retrieving them from the `Namespace` object, e.g., `double` or `boolean`.\n* `metaVar(String)` -- for setting the display string for the argument of an\n  option (uses upper case). By default, the `destination` string is used.\n\n## Parsing options\n\nThe following snippet parses the options (`args`) and returns them as\n`Namespace` object (`parseArgs(String[])` method). \nIn case of invalid options, missing required options\nor help being request, an `ArgumentParserException` is thrown.\nUsing the parser's `handleError(ArgumentParserException)` you can output\na help screen generated from the defined options.\n\n```java\nimport com.github.fracpete.simpleargparse4j.ArgumentParserException;\nimport com.github.fracpete.simpleargparse4j.Namespace;\n...\nNamespace ns;\ntry {\n  ns = parser.parseArgs(args);\n}\ncatch (ArgumentParserException e) {\n  parser.handleError(e);\n  return !parser.getHelpRequested();  // help request is valid operation\n}\n```\n\nUsing `parseArgs(String[],true)` you can remove the all the parsed options\nfrom the provided string array. `parseArgs(String[])` does not remove them\nby default.\n\n\n## Retrieving parsed values\n\nOnce the options have been parsed, you can retrieve (typed) from the\n`Namespace` object:\n\n* `getString(String)` -- returns the string associated with the provided key\n* `getBoolean(String)` -- returns the boolean associated with the provided key; \n  whether a `true` or `false` gets returned is determine by the default value.\n  Without specifying an explicit default value, `false` gets returned if the\n  flag is present, otherwise `true`. You can invert this by simply using \n  `setDefault(true)` when defining the option.\n* `getByte(String)` -- returns the byte associated with the provided key\n* `getShort(String)` -- returns the short associated with the provided key\n* `getInt(String)` -- returns the integer associated with the provided key\n* `getLong(String)` -- returns the long associated with the provided key\n* `getFloat(String)` -- returns the float associated with the provided key\n* `getDouble(String)` -- returns the double associated with the provided key\n* `getFile(String)` -- returns the double associated with the provided key\n* `getList(String)` -- returns the list associated with the provided key\n\n\n## Example\n\nThe following example configures several parameters, not all of them required.\nIt then parses the arguments provided to the application and outputs the\nparsed values.\n\n```java\nimport com.github.fracpete.simpleargparse4j.ArgumentParser;\nimport com.github.fracpete.simpleargparse4j.ArgumentParserException;\nimport com.github.fracpete.simpleargparse4j.Namespace;\n\npublic static void main(String[] args) {\n  // define the options\n  ArgumentParser parser = new ArgumentParser(\"create\");\n  parser.addOption(\"--name\")\n    .dest(\"name\")\n    .help(\"the name of the environment\")\n    .required(true);\n  parser.addOption(\"--java\")\n    .dest(\"java\")\n    .help(\"the full path of the java binary to use for launching Weka\")\n    .setDefault(\"\");\n  parser.addOption(\"--memory\")\n    .dest(\"memory\")\n    .help(\"the heap size to use for launching Weka (eg '1024m' or '2g')\")\n    .setDefault(\"\");\n  parser.addOption(\"--weka\")\n    .dest(\"weka\")\n    .help(\"the full path to the weka.jar to use\")\n    .required(true);\n  parser.addOption(\"--wekafiles\")\n    .dest(\"wekafiles\")\n    .help(\"the full path to the 'wekafiles' directory to initialize the environment with\")\n    .setDefault(\"\");\n  parser.addOption(\"--envvar\")\n    .dest(\"envvar\")\n    .help(\"optional environment variables to set (key=value)\")\n    .multiple(true);\n  \n  // parse the options\n  Namespace ns;\n  try {\n    ns = parser.parseArgs(args, true);\n  }\n  catch (ArgumentParserException e) {\n    parser.handleError(e);\n    return false;\n  }\n\n  // output the parsed values\n  System.out.println(\"Name: \" + ns.getString(\"name\"));\n  System.out.println(\"Jav: \" + ns.getString(\"java\"));\n  System.out.println(\"Memory: \" + ns.getString(\"memory\"));\n  System.out.println(\"Weka jar: \" + ns.getString(\"weka\"));\n  System.out.println(\"Env. vars: \" + ns.getList(\"envvar\"));\n}\n```\n\n## Maven\n\nAdd the following dependency to your `pom.xml`:\n\n```xml\n    \u003cdependency\u003e\n      \u003cgroupId\u003ecom.github.fracpete\u003c/groupId\u003e\n      \u003cartifactId\u003esimple-argparse4j\u003c/artifactId\u003e\n      \u003cversion\u003e0.0.13\u003c/version\u003e\n    \u003c/dependency\u003e\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffracpete%2Fsimple-argparse4j","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffracpete%2Fsimple-argparse4j","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffracpete%2Fsimple-argparse4j/lists"}