{"id":15669696,"url":"https://github.com/shasait/clap","last_synced_at":"2025-05-06T20:06:49.503Z","repository":{"id":6482048,"uuid":"7722270","full_name":"shasait/clap","owner":"shasait","description":"Command Line Arguments Parser for Java","archived":false,"fork":false,"pushed_at":"2022-11-29T18:11:09.000Z","size":348,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-31T02:22:34.146Z","etag":null,"topics":["annotations","apache-license-2","argument-parser","i18n","java"],"latest_commit_sha":null,"homepage":null,"language":"Java","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/shasait.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2013-01-20T21:34:16.000Z","updated_at":"2024-06-03T19:15:22.000Z","dependencies_parsed_at":"2023-01-12T15:01:29.339Z","dependency_job_id":null,"html_url":"https://github.com/shasait/clap","commit_stats":null,"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shasait%2Fclap","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shasait%2Fclap/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shasait%2Fclap/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shasait%2Fclap/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shasait","download_url":"https://codeload.github.com/shasait/clap/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252761143,"owners_count":21800124,"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":["annotations","apache-license-2","argument-parser","i18n","java"],"created_at":"2024-10-03T14:40:48.248Z","updated_at":"2025-05-06T20:06:49.481Z","avatar_url":"https://github.com/shasait.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"clap\n====\n\n[![Maven Central](https://img.shields.io/maven-central/v/de.hasait/clap.svg?label=Maven%20Central)](http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22de.hasait%22%20AND%20a%3A%22clap%22)\n\nCommand Line Arguments Parser for Java\n\n* Licensed under the Apache License, Version 2.0\n* Supports *NIX style options\n* Short options: -v\n* Long options: --verbose\n* Options with zero arguments (flags): -v\n* Options with exact number of arguments: -p22 --port=22 --port 22 --sum=1;6\n* Options with unlimited number of arguments\n* Decisions/Alternatives (e.g. to support different commands)\n* Keywords\n* Nameless option\n* Generified for type safety\n* Custom type converters\n* Imperative style (e.g. addOption, addFlag, ...)\n* Annotations (@Option, @HelpCategory, ...)\n* Annotations can be mixed with imperative style\n* Mapping of arguments to nested data structures\n* I18N for usage and error messages\n* Customizable usage and help categories\n* Since 3.0.0 support for JDK17\n\n## Examples\n\n### Basic usage\n\n```java\nimport de.hasait.clap.*;\n\npublic class BasicCLI {\n    public static void main(String[] args) {\n        CLAP clap = new CLAP();\n        CLAPValue\u003cBoolean\u003e verboseOption = clap.addFlag('v', \"verbose\", false, \"Increase verbosity level\");\n        CLAPValue\u003cBoolean\u003e helpOption = clap.addFlag('h', \"help\", false, \"Print help\", true);\n\n        CLAPResult result = clap.parse(args);\n\n        if (result.contains(helpOption)) {\n            clap.printUsageAndHelp(System.out);\n            return;\n        }\n\n        int verbosityLevel = result.getCount(verboseOption);\n\n        // TODO do something useful\n    }\n}\n```\n\n### Decisions/Alternatives\n\nExample with two exclusive sets of options: One for acting as client and another one for acting as server.\n\n```java\nimport de.hasait.clap.*;\n\npublic class ClientServerCLI {\n    public static void main(String[] args) {\n        // Usage: [-v|--verbose] [-h|--help] { {-H|--host \u003chost\u003e} {-p|--port \u003cport\u003e} | {-l|--listen \u003cport\u003e} }\n\n        CLAP clap = new CLAP();\n        CLAPValue\u003cBoolean\u003e verboseOption = clap.addFlag('v', \"verbose\", false, \"Increase verbosity level\");\n        CLAPValue\u003cBoolean\u003e helpOption = clap.addFlag('h', \"help\", false, \"Print help\", true);\n        CLAPNode decision = clap.addDecision();\n        CLAPNode clientBranch = decision.addGroup();\n        clientBranch.setHelpCategory(2000, \"Client\");\n        CLAPValue\u003cString\u003e clientHost = clientBranch.addOption1(String.class, 'H', \"host\", true, \"The host to connect to\", \"host\");\n        CLAPValue\u003cInteger\u003e clientPort = clientBranch.addOption1(Integer.class, 'p', \"port\", true, \"The port to connect to\", \"port\");\n\n        CLAPNode serverBranch = decision.addGroup();\n        serverBranch.setHelpCategory(2001, \"Server\");\n        CLAPValue\u003cInteger\u003e serverPort = serverBranch.addOption1(Integer.class, 'l', \"listen\", true, \"The port to listen on\", \"port\");\n\n        CLAPResult result;\n        try {\n            result = clap.parse(args);\n        } catch (CLAPException e) {\n            clap.printUsageAndHelp(System.out);\n            throw e;\n        }\n\n        if (result.contains(helpOption)) {\n            clap.printUsageAndHelp(System.out);\n            return;\n        }\n\n        int verbosityLevel = result.getCount(verboseOption);\n        System.out.println(\"verbosityLevel=\" + verbosityLevel);\n\n        if (result.contains(clientBranch)) {\n            System.out.println(\"Connecting to \" + result.getValue(clientHost) + \":\" + result.getValue(clientPort) + \"...\");\n            // TODO client\n        } else if (result.contains(serverBranch)) {\n            System.out.println(\"Listening on \" + result.getValue(serverPort) + \"...\");\n            // TODO server\n        }\n\n    }\n}\n```\n\n### Commands using Annotations\n\nExample program supporting two commands: Scale and rotate of provided files.\n\n```java\nimport de.hasait.clap.*;\n\npublic class CommandCLI {\n\n    public interface Command {\n\n        void execute(String[] files);\n\n    }\n\n    @CLAPKeyword(\"rotate\")\n    public static class RotateCommand implements Command {\n\n        private boolean ccw;\n\n        public boolean isCcw() {\n            return ccw;\n        }\n\n        @CLAPOption(longKey = \"ccw\", descriptionNLSKey = \"Rotate counterclockwise\")\n        public void setCcw(boolean ccw) {\n            this.ccw = ccw;\n        }\n\n        @Override\n        public void execute(String[] files) {\n            System.out.println(\"rotate \" + (ccw ? \"ccw\" : \"cw\") + \": \" + Arrays.asList(files));\n        }\n\n    }\n\n    @CLAPKeyword(\"scale\")\n    public static class ScaleCommand implements Command {\n\n        private int percent;\n\n        public int getPercent() {\n            return percent;\n        }\n\n        @CLAPOption(shortKey = 'p', longKey = \"percent\", required = true, descriptionNLSKey = \"Scale percentage\", argUsageNLSKey = \"percent\")\n        public void setPercent(int percent) {\n            this.percent = percent;\n        }\n\n        @Override\n        public void execute(String[] files) {\n            System.out.println(\"scale \" + percent + \"%: \" + Arrays.asList(files));\n        }\n\n    }\n\n    public static class Args {\n\n        private Command command;\n\n        private String[] files;\n\n        public Command getCommand() {\n            return command;\n        }\n\n        @CLAPDecision(branches = {\n                ScaleCommand.class,\n                RotateCommand.class\n        })\n        public void setCommand(Command command) {\n            this.command = command;\n        }\n\n        public String[] getFiles() {\n            return files;\n        }\n\n        @CLAPOption(required = true, descriptionNLSKey = \"Files to process\", argUsageNLSKey = \"file\")\n        public void setFiles(String[] files) {\n            this.files = files;\n        }\n\n    }\n\n    public static void main(String[] rawArgs) {\n        CLAP clap = new CLAP();\n        CLAPValue\u003cBoolean\u003e verboseOption = clap.addFlag('v', \"verbose\", false, \"Increase verbosity level\");\n        CLAPValue\u003cBoolean\u003e helpOption = clap.addFlag('h', \"help\", false, \"Print help\", true);\n        CLAPValue\u003cArgs\u003e argsClassOption = clap.addClass(Args.class);\n\n        CLAPResult result;\n        try {\n            result = clap.parse(rawArgs);\n        } catch (CLAPException e) {\n            clap.printUsageAndHelp(System.out);\n            throw e;\n        }\n\n        if (result.contains(helpOption)) {\n            clap.printUsageAndHelp(System.out);\n            return;\n        }\n\n        int verbosityLevel = result.getCount(verboseOption);\n        System.out.println(\"verbosityLevel=\" + verbosityLevel);\n\n        Args args = result.getValue(argsClassOption);\n        args.getCommand().execute(args.files);\n    }\n\n}\n```\n\n### More examples\n\nPlease have a look at the various [tests](https://github.com/shasait/clap/tree/master/src/test/java/de/hasait/clap). You will also find\nexamples for annotation based parsing.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshasait%2Fclap","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshasait%2Fclap","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshasait%2Fclap/lists"}