Skip to content

Commit

Permalink
feat: support -h\--help option (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
sukidayou authored Nov 21, 2024
1 parent 03a9a9d commit 49231e0
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/main/java/org/casbin/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public static String run(String... args) {
}

String commandName = args[0];
if(Objects.equals(commandName, "-h") || Objects.equals(commandName, "--help")){
printHelpMessage();
return result;
}

CommandLine cmd = getCmd(Arrays.copyOfRange(args, 1, args.length));
String model = cmd.getOptionValue("model");
Expand All @@ -40,6 +44,7 @@ public static String run(String... args) {

} catch (Exception e) {
e.printStackTrace();
System.out.println("Run './casbin --help or ./casbin -h' for usage.");
System.exit(1);
}
return result;
Expand Down Expand Up @@ -77,4 +82,36 @@ private static CommandLine getCmd(String[] args) throws ParseException {
CommandLineParser parser = new DefaultParser();
return parser.parse(options, args);
}

private static void printHelpMessage() {
System.out.println(" Usage: ./casbin [Method] [options] [args]\n" +
"\n" +
" Casbin is a powerful and efficient open-source access control library.\n" +
" It provides support for enforcing authorization based on various access control models.\n" +
"\n" +
" Method:\n" +
" enforce Test if a 'subject' can access an 'object' with a given 'action' based on the policy\n" +
" enforceEx Check permissions and get which policy it matches\n" +
" addFunction Add custom function\n" +
" addPolicy Add a policy rule to the policy file\n" +
" removePolicy Remove a policy rule from the policy file\n" +
" For more method, visit https://github.com/casbin/jcasbin\n" +
"\n" +
" Options:\n" +
" -m, --model <model> The path of the model file or model text. Please wrap it with \"\" and separate each line with \"|\"\n" +
" -p, --policy <policy> The path of the policy file or policy text. Please wrap it with \"\" and separate each line with \"|\"\n" +
" -AF, --addFunction <functionDefinition> Add custom function. Please wrap it with \"\" and separate each line with \"|\"\n" +
"\n" +
" args:\n" +
" Parameters required for the method\n" +
"\n" +
" Examples:\n" +
" ./casbin enforce -m \"examples/rbac_model.conf\" -p \"examples/rbac_policy.csv\" \"alice\" \"data1\" \"read\"\n" +
" ./casbin enforceEx -m \"examples/rbac_model.conf\" -p \"examples/rbac_policy.csv\" \"alice\" \"data1\" \"read\"\n" +
" ./casbin addPolicy -m \"examples/rbac_model.conf\" -p \"examples/rbac_policy.csv\" \"alice\" \"data2\" \"write\"\n" +
" ./casbin enforce -m \"your_model.conf\" -p \"examples/keymatch_policy.csv\" -AF \"yourFunctionDefinition\" \"alice\" \"/alice_data/resource1\" \"GET\"\n" +
"\n" +
" For more information, visit https://github.com/casbin/casbin");

}
}

0 comments on commit 49231e0

Please sign in to comment.