diff --git a/src/main/java/org/casbin/Client.java b/src/main/java/org/casbin/Client.java index 54773a6..b404d78 100644 --- a/src/main/java/org/casbin/Client.java +++ b/src/main/java/org/casbin/Client.java @@ -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"); @@ -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; @@ -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 The path of the model file or model text. Please wrap it with \"\" and separate each line with \"|\"\n" + + " -p, --policy The path of the policy file or policy text. Please wrap it with \"\" and separate each line with \"|\"\n" + + " -AF, --addFunction 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"); + + } }