Skip to content

Commit 7f977ff

Browse files
committed
feat: support -h\--help option
1 parent 03a9a9d commit 7f977ff

File tree

1 file changed

+61
-1
lines changed

1 file changed

+61
-1
lines changed

src/main/java/org/casbin/Client.java

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33

44
import org.apache.commons.cli.*;
55
import org.casbin.generate.DynamicClassGenerator;
6+
import org.casbin.jcasbin.exception.CasbinMatcherException;
67
import org.casbin.jcasbin.util.function.CustomFunction;
78
import org.casbin.util.Util;
89

10+
import java.lang.reflect.InvocationTargetException;
911
import java.util.*;
1012

1113

@@ -20,6 +22,10 @@ public static String run(String... args) {
2022
}
2123

2224
String commandName = args[0];
25+
if(Objects.equals(commandName, "-h") || Objects.equals(commandName, "--help")){
26+
printHelpMessage();
27+
return result;
28+
}
2329

2430
CommandLine cmd = getCmd(Arrays.copyOfRange(args, 1, args.length));
2531
String model = cmd.getOptionValue("model");
@@ -39,10 +45,32 @@ public static String run(String... args) {
3945
return o.toString();
4046

4147
} catch (Exception e) {
48+
handleException(e, args);
49+
return result;
50+
}
51+
}
52+
53+
/**
54+
* Handles exceptions and prints appropriate error messages.
55+
*
56+
* @param e the exception to handle
57+
* @param args the arguments passed to the command
58+
*/
59+
private static void handleException(Exception e, String[] args) {
60+
if (e instanceof MissingOptionException) {
61+
System.out.println("Error: Missing required options. Please provide the necessary arguments.");
62+
} else if (e instanceof ParseException) {
63+
System.out.println("Error: Invalid command-line arguments. " + e.getMessage());
64+
} else if (e instanceof IllegalArgumentException) {
65+
System.out.println("Error: " + e.getMessage());
66+
} else if (e instanceof InvocationTargetException && e.getCause() instanceof CasbinMatcherException) {
67+
System.out.println("Error: " + e.getCause().getMessage());
68+
} else {
69+
System.out.println("Error: unknown command \"" + Arrays.toString(args) + "\" for \"casbin\"");
4270
e.printStackTrace();
4371
System.exit(1);
4472
}
45-
return result;
73+
System.out.println("Run './casbin --help or ./casbin -h' for usage.");
4674
}
4775

4876

@@ -77,4 +105,36 @@ private static CommandLine getCmd(String[] args) throws ParseException {
77105
CommandLineParser parser = new DefaultParser();
78106
return parser.parse(options, args);
79107
}
108+
109+
private static void printHelpMessage() {
110+
System.out.println(" Usage: ./casbin [Method] [options] [args]\n" +
111+
"\n" +
112+
" Casbin is a powerful and efficient open-source access control library.\n" +
113+
" It provides support for enforcing authorization based on various access control models.\n" +
114+
"\n" +
115+
" Method:\n" +
116+
" enforce Test if a 'subject' can access an 'object' with a given 'action' based on the policy\n" +
117+
" enforceEx Check permissions and get which policy it matches\n" +
118+
" addFunction Add custom function\n" +
119+
" addPolicy Add a policy rule to the policy file\n" +
120+
" removePolicy Remove a policy rule from the policy file\n" +
121+
" For more method, visit https://github.com/casbin/jcasbin\n" +
122+
"\n" +
123+
" Options:\n" +
124+
" -m, --model <model> The path of the model file or model text. Please wrap it with \"\" and separate each line with \"|\"\n" +
125+
" -p, --policy <policy> The path of the policy file or policy text. Please wrap it with \"\" and separate each line with \"|\"\n" +
126+
" -AF, --addFunction <functionDefinition> Add custom function. Please wrap it with \"\" and separate each line with \"|\"\n" +
127+
"\n" +
128+
" args:\n" +
129+
" Parameters required for the method\n" +
130+
"\n" +
131+
" Examples:\n" +
132+
" ./casbin enforce -m \"examples/rbac_model.conf\" -p \"examples/rbac_policy.csv\" \"alice\" \"data1\" \"read\"\n" +
133+
" ./casbin enforceEx -m \"examples/rbac_model.conf\" -p \"examples/rbac_policy.csv\" \"alice\" \"data1\" \"read\"\n" +
134+
" ./casbin addPolicy -m \"examples/rbac_model.conf\" -p \"examples/rbac_policy.csv\" \"alice\" \"data2\" \"write\"\n" +
135+
" ./casbin enforce -m \"your_model.conf\" -p \"examples/keymatch_policy.csv\" -AF \"yourFunctionDefinition\" \"alice\" \"/alice_data/resource1\" \"GET\"\n" +
136+
"\n" +
137+
" For more information, visit https://github.com/casbin/casbin");
138+
139+
}
80140
}

0 commit comments

Comments
 (0)