33
44import org .apache .commons .cli .*;
55import org .casbin .generate .DynamicClassGenerator ;
6+ import org .casbin .jcasbin .exception .CasbinMatcherException ;
67import org .casbin .jcasbin .util .function .CustomFunction ;
78import org .casbin .util .Util ;
89
10+ import java .lang .reflect .InvocationTargetException ;
911import 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" );
@@ -38,7 +44,29 @@ public static String run(String... args) {
3844 System .out .println (o );
3945 return o .toString ();
4046
47+ } catch (MissingOptionException e ) {
48+ System .out .println ("Error: Missing required options. Please provide the necessary arguments.\n " +
49+ "Run './casbin --help or ./casbin -h' for usage." );
50+ } catch (ParseException e ) {
51+ System .out .println ("Error: Invalid command-line arguments. " + e .getMessage () + "\n " +
52+ "Run './casbin --help or ./casbin -h' for usage" );
53+ } catch (IllegalArgumentException e ) {
54+ System .out .println ("Error: " + e .getMessage ());
55+ System .out .println ("Run './casbin --help or ./casbin -h' for usage." );
56+ } catch (InvocationTargetException e ){
57+ Throwable cause = e .getCause ();
58+ if (cause instanceof CasbinMatcherException ) {
59+ System .out .println ("Error: " + cause .getMessage ());
60+ System .out .println ("Run './casbin --help or ./casbin -h' for usage." );
61+ } else {
62+ System .out .println ("Error: unknown command \" " + Arrays .toString (args ) + "\" for \" casbin\" " );
63+ System .out .println ("Run './casbin --help or ./casbin -h' for usage." );
64+ e .printStackTrace ();
65+ System .exit (1 );
66+ }
4167 } catch (Exception e ) {
68+ System .out .println ("Error: unknown command \" " + Arrays .toString (args ) + "\" for \" casbin\" " );
69+ System .out .println ("Run './casbin --help or ./casbin -h' for usage." );
4270 e .printStackTrace ();
4371 System .exit (1 );
4472 }
@@ -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