Skip to content

Commit f197cf5

Browse files
committed
feat: add -h\--help option and update README content
1 parent ebc0018 commit f197cf5

File tree

2 files changed

+79
-20
lines changed

2 files changed

+79
-20
lines changed

README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,8 @@ mvn clean install
2121
| options | description | must | remark |
2222
|-----------------------|----------------------------------------------|------|-----------------------------------------------------------|
2323
| `-m, --model` | The path of the model file or model text | y | Please wrap it with `""` and separate each line with `\|` |
24-
| `-p, --policy` | The path of the policy file or policy text | y | Please wrap it with `""` and separate each line with `\|` |
25-
| `-e, --enforce` | Check permissions | n | Please wrap it with `""` |
26-
| `-ex, --enforceEx` | Check permissions and get which policy it is | n | Please wrap it with `""` |
24+
| `-p, --policy` | The path of the policy file or policy text | y | Please wrap it with `""` and separate each line with `\|` |
2725
| `-AF, --addFuntion` | Add custom funtion | n | Please wrap it with `""` and separate each line with `\|` |
28-
| `-ap, --addPolicy` | Add a policy rule to the policy file | n | Please wrap it with `""` |
29-
| `-rp, --removePolicy` | Remove a policy rule from the policy file | n | Please wrap it with `""` |
3026

3127
## Get started
3228

Lines changed: 78 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
1+
// Copyright 2024 The casbin Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
115
package org.casbin;
216

317

418
import org.apache.commons.cli.*;
519
import org.casbin.generate.DynamicClassGenerator;
20+
import org.casbin.jcasbin.exception.CasbinMatcherException;
621
import org.casbin.jcasbin.util.function.CustomFunction;
722
import org.casbin.util.Util;
823

24+
import java.lang.reflect.InvocationTargetException;
925
import java.util.*;
1026

1127

@@ -16,44 +32,58 @@ public static String run(String... args) {
1632

1733
try {
1834
if(args == null || args.length == 0) {
19-
printUsageMessageAndExit("");
35+
printHelpMessage();
2036
}
2137

2238
String commandName = args[0];
39+
if(Objects.equals(commandName, "-h") || Objects.equals(commandName, "--help")){
40+
printHelpMessage();
41+
return result;
42+
}
2343

2444
CommandLine cmd = getCmd(Arrays.copyOfRange(args, 1, args.length));
45+
2546
String model = cmd.getOptionValue("model");
2647
String policy = cmd.getOptionValue("policy");
2748
NewEnforcer enforcer = new NewEnforcer(model, policy);
2849

29-
3050
if(cmd.hasOption("AF")) {
3151
String codes = cmd.getOptionValue("AF");
3252
String methodName = Util.getMethodName(codes);
3353
CustomFunction customFunction = DynamicClassGenerator.generateClass(methodName, codes);
3454
enforcer.addFunction(methodName, customFunction);
3555
}
56+
3657
CommandExecutor commandExecutor = new CommandExecutor(enforcer, commandName, cmd.getArgs());
3758
Object o = commandExecutor.outputResult();
3859
System.out.println(o);
3960
return o.toString();
4061

62+
} catch (MissingOptionException e) {
63+
System.out.println("Error: Missing required options. Please provide the necessary arguments.");
64+
System.out.println("Run './casbin --help or ./casbin -h' for usage.");
65+
} catch (ParseException e) {
66+
System.out.println("Error: Invalid command-line arguments. " + e.getMessage());
67+
System.out.println("Run './casbin --help or ./casbin -h' for usage.");
68+
} catch (RuntimeException e) {
69+
System.out.println("Error: " + e.getMessage());
70+
System.out.println("Run './casbin --help or ./casbin -h' for usage.");
71+
} catch (InvocationTargetException e){
72+
Throwable cause = e.getCause();
73+
if (cause instanceof CasbinMatcherException) {
74+
System.out.println("Error: " + cause.getMessage());
75+
System.out.println("Run './casbin --help or ./casbin -h' for usage.");
76+
} else {
77+
System.out.println("Error: unknown command \"" + Arrays.toString(args) + "\" for \"casbin\"");
78+
System.out.println("Run './casbin --help or ./casbin -h' for usage.");
79+
}
4180
} catch (Exception e) {
42-
e.printStackTrace();
43-
System.exit(1);
81+
System.out.println("Error: unknown command \"" + Arrays.toString(args) + "\" for \"casbin\"");
82+
System.out.println("Run './casbin --help or ./casbin -h' for usage.");
4483
}
4584
return result;
4685
}
4786

48-
49-
private static void printUsageMessageAndExit(String commandName) throws Exception {
50-
if (commandName.isEmpty()) {
51-
System.out.println("Error: " + commandName + " not recognised");
52-
}
53-
// new HelpCommand().run();
54-
System.exit(1);
55-
}
56-
5787
public static void main(String[] args) throws ParseException {
5888
run(args);
5989
}
@@ -67,14 +97,47 @@ private static CommandLine getCmd(String[] args) throws ParseException {
6797
options.addOption(option);
6898

6999
option = new Option("m", "model", true, "the path of the model file or model text");
70-
option.hasArg();
100+
option.setArgs(1);
101+
option.setRequired(true);
71102
options.addOption(option);
72103

73104
option = new Option("p", "policy", true, "the path of the policy file or policy text");
74-
option.hasArg();
105+
option.setArgs(1);
106+
option.setRequired(true);
75107
options.addOption(option);
76108

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

0 commit comments

Comments
 (0)