Skip to content

Commit

Permalink
feat: support -h\--help option
Browse files Browse the repository at this point in the history
  • Loading branch information
sukidayou committed Nov 21, 2024
1 parent 03a9a9d commit 7f977ff
Showing 1 changed file with 61 additions and 1 deletion.
62 changes: 61 additions & 1 deletion src/main/java/org/casbin/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

import org.apache.commons.cli.*;
import org.casbin.generate.DynamicClassGenerator;
import org.casbin.jcasbin.exception.CasbinMatcherException;
import org.casbin.jcasbin.util.function.CustomFunction;
import org.casbin.util.Util;

import java.lang.reflect.InvocationTargetException;
import java.util.*;


Expand All @@ -20,6 +22,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 @@ -39,10 +45,32 @@ public static String run(String... args) {
return o.toString();

} catch (Exception e) {
handleException(e, args);
return result;
}
}

/**
* Handles exceptions and prints appropriate error messages.
*
* @param e the exception to handle
* @param args the arguments passed to the command
*/
private static void handleException(Exception e, String[] args) {
if (e instanceof MissingOptionException) {
System.out.println("Error: Missing required options. Please provide the necessary arguments.");
} else if (e instanceof ParseException) {
System.out.println("Error: Invalid command-line arguments. " + e.getMessage());
} else if (e instanceof IllegalArgumentException) {
System.out.println("Error: " + e.getMessage());
} else if (e instanceof InvocationTargetException && e.getCause() instanceof CasbinMatcherException) {
System.out.println("Error: " + e.getCause().getMessage());
} else {
System.out.println("Error: unknown command \"" + Arrays.toString(args) + "\" for \"casbin\"");
e.printStackTrace();
System.exit(1);
}
return result;
System.out.println("Run './casbin --help or ./casbin -h' for usage.");
}


Expand Down Expand Up @@ -77,4 +105,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 7f977ff

Please sign in to comment.