Overhaul the argument parsing API
This commit is contained in:
parent
62d26082c9
commit
82f5c18129
9 changed files with 207 additions and 153 deletions
|
@ -3,7 +3,6 @@ import 'package:libac_dart/argparse/types/Integers.dart';
|
|||
import 'package:libac_dart/argparse/types/String.dart';
|
||||
|
||||
abstract class Argument<T> {
|
||||
bool hasValue = false;
|
||||
String name;
|
||||
|
||||
String description;
|
||||
|
@ -46,53 +45,75 @@ abstract class Argument<T> {
|
|||
}
|
||||
|
||||
ArgumentType getType();
|
||||
|
||||
bool hasValue();
|
||||
}
|
||||
|
||||
enum ArgumentType { STRING, BOOL, INTEGER, DOUBLE }
|
||||
|
||||
class Arguments {
|
||||
Map<String, Argument> _args = {};
|
||||
Map<String, Argument> args = {};
|
||||
|
||||
void setArg(Argument arg) {
|
||||
_args[arg.name] = arg;
|
||||
args[arg.name] = arg;
|
||||
}
|
||||
|
||||
int get count => _args.length;
|
||||
int get count => args.length;
|
||||
|
||||
List<String> getArgNames() {
|
||||
List<String> args = [];
|
||||
for (MapEntry<String, Argument> entry in _args.entries) {
|
||||
args.add(entry.key);
|
||||
List<String> argsx = [];
|
||||
for (MapEntry<String, Argument> entry in args.entries) {
|
||||
argsx.add(entry.key);
|
||||
}
|
||||
|
||||
return args;
|
||||
return argsx;
|
||||
}
|
||||
|
||||
/// Removes the argument if present. Otherwise does nothing.
|
||||
///
|
||||
/// Returns true if the argument was present and removed. False otherwise.
|
||||
bool removeArg(String name) {
|
||||
if (args.containsKey(name)) {
|
||||
args.remove(name);
|
||||
return true;
|
||||
} else
|
||||
return false;
|
||||
}
|
||||
|
||||
Argument? getArg(String name) {
|
||||
if (hasArg(name))
|
||||
return _args[name];
|
||||
return args[name];
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
bool hasArg(String name) {
|
||||
return _args.containsKey(name);
|
||||
return args.containsKey(name);
|
||||
}
|
||||
|
||||
bool argHasValue(String name) {
|
||||
if (hasArg(name)) {
|
||||
Argument arg = getArg(name)!;
|
||||
return arg.hasValue;
|
||||
return arg.hasValue();
|
||||
} else
|
||||
throw new Exception("Warning: No such argument");
|
||||
}
|
||||
|
||||
Arguments clone() {
|
||||
Arguments args = Arguments();
|
||||
for (MapEntry<String, Argument> entry in _args.entries) {
|
||||
args.setArg(entry.value.clone());
|
||||
Arguments argsx = Arguments();
|
||||
for (MapEntry<String, Argument> entry in args.entries) {
|
||||
argsx.setArg(entry.value.clone());
|
||||
}
|
||||
|
||||
return args;
|
||||
return argsx;
|
||||
}
|
||||
|
||||
List<Argument> getArgumentsList() {
|
||||
List<Argument> argsx = [];
|
||||
for (var entry in args.entries) {
|
||||
argsx.add(entry.value);
|
||||
}
|
||||
|
||||
return argsx;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,109 +1,51 @@
|
|||
import 'package:libac_dart/argparse/types/Bool.dart';
|
||||
import 'package:libac_dart/argparse/types/Integers.dart';
|
||||
import 'package:libac_dart/argparse/types/String.dart';
|
||||
import 'package:libac_dart/nbt/Stream.dart';
|
||||
import 'package:libac_dart/argparse/Args.dart';
|
||||
|
||||
import 'Args.dart';
|
||||
|
||||
class CLIHelper {
|
||||
/// Generates usage info
|
||||
class ArgumentHelpers {
|
||||
/// Generates a command-line help message for the provided arguments.
|
||||
///
|
||||
/// This will create a standard CLI Usage info string that can be directly printed to the console after the program header
|
||||
static String makeArgCLIHelp(Arguments defaults) {
|
||||
StringBuilder builder = StringBuilder();
|
||||
List<String> argNames = defaults.getArgNames();
|
||||
/// [arguments] The list of arguments to generate help for.
|
||||
/// [programName] The name of the program.
|
||||
/// Returns a string containing the help message.
|
||||
static String generateHelpMessage(
|
||||
List<Argument<dynamic>> arguments, String programName) {
|
||||
final StringBuffer helpMessage = StringBuffer();
|
||||
helpMessage.writeln('Usage: $programName [options]');
|
||||
|
||||
for (String name in argNames) {
|
||||
builder.append("--${name}");
|
||||
|
||||
Argument arg = defaults.getArg(name)!;
|
||||
if (arg.hasValue) {
|
||||
builder.append("=<...>");
|
||||
}
|
||||
|
||||
builder.append(
|
||||
"\t\t\t${arg.description} ${(arg.getValue().toString() == "%" || arg.getType() == ArgumentType.BOOL) ? "" : "[Default: ${arg.getValue()}]"}\n");
|
||||
for (var arg in arguments) {
|
||||
final description = _getArgumentDescription(arg);
|
||||
final valueType = arg.getType().toString();
|
||||
helpMessage.writeln(
|
||||
' --${arg.name} [${valueType.split('.').last}] $description');
|
||||
}
|
||||
|
||||
return builder.toString();
|
||||
return helpMessage.toString();
|
||||
}
|
||||
|
||||
/// Parses and returns the arguments list with keeping defaults in mind
|
||||
static Future<Arguments> parseArgs(
|
||||
List<String> args, Arguments defaults) async {
|
||||
Arguments arguments = defaults.clone();
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
Argument arg = await _parseArgument(args[i]);
|
||||
Argument? defArg;
|
||||
if (defaults.hasArg(arg.name)) defArg = defaults.getArg(arg.name)!;
|
||||
/// Gets a description for an argument. This can be extended to provide more info.
|
||||
///
|
||||
/// [argument] The argument for which to generate a description.
|
||||
/// Returns a description of the argument.
|
||||
static String _getArgumentDescription(Argument<dynamic> argument) {
|
||||
// You can extend this to add more detailed descriptions for specific arguments
|
||||
return argument.hasValue()
|
||||
? 'Default value: ${argument.getValue()}'
|
||||
: 'No default value assigned';
|
||||
}
|
||||
|
||||
if (!arg.hasValue) {
|
||||
if (defArg != null) {
|
||||
arg = defArg;
|
||||
}
|
||||
}
|
||||
|
||||
arguments.setArg(arg);
|
||||
/// Retrieves the type of an argument in a human-readable format.
|
||||
///
|
||||
/// [argument] The argument to get the type for.
|
||||
/// Returns a string describing the argument type.
|
||||
static String _getArgumentType(Argument<dynamic> argument) {
|
||||
switch (argument.getType()) {
|
||||
case ArgumentType.STRING:
|
||||
return 'string';
|
||||
case ArgumentType.INTEGER:
|
||||
return 'integer';
|
||||
case ArgumentType.BOOL:
|
||||
return 'boolean';
|
||||
case ArgumentType.DOUBLE:
|
||||
return 'double';
|
||||
}
|
||||
|
||||
return arguments;
|
||||
}
|
||||
|
||||
static Future<Argument> _parseArgument(String arg) async {
|
||||
if (arg.startsWith("--")) {
|
||||
var parts = arg.split("=");
|
||||
String name = await _getNamePart(parts[0]);
|
||||
if (parts.length == 1)
|
||||
return BoolArgument(name: name);
|
||||
else if (parts.length == 2) {
|
||||
String value = await _getValuePart(parts[1]);
|
||||
ArgumentType typeOfArg = await _getArgumentType(value);
|
||||
|
||||
switch (typeOfArg) {
|
||||
case ArgumentType.INTEGER:
|
||||
{
|
||||
return IntegerArgument(name: name, value: int.parse(value));
|
||||
}
|
||||
case ArgumentType.BOOL:
|
||||
{
|
||||
return BoolArgument(name: name);
|
||||
}
|
||||
case ArgumentType.DOUBLE:
|
||||
{
|
||||
return DoubleArgument(name: name, value: double.parse(value));
|
||||
}
|
||||
case ArgumentType.STRING:
|
||||
{
|
||||
return StringArgument(name: name, value: value);
|
||||
}
|
||||
}
|
||||
} else
|
||||
throw new Exception("Argument is malformed");
|
||||
} else
|
||||
throw new Exception("Not in a valid format");
|
||||
}
|
||||
|
||||
static Future<String> _getNamePart(String value) async {
|
||||
return value.substring(2);
|
||||
}
|
||||
|
||||
static Future<String> _getValuePart(String entry) async {
|
||||
return entry;
|
||||
}
|
||||
|
||||
static Future<ArgumentType> _getArgumentType(String input) async {
|
||||
try {
|
||||
int.parse(input);
|
||||
return ArgumentType.INTEGER;
|
||||
} catch (E) {}
|
||||
try {
|
||||
double.parse(input);
|
||||
return ArgumentType.DOUBLE;
|
||||
} catch (E) {}
|
||||
|
||||
if (input.isEmpty)
|
||||
return ArgumentType.BOOL;
|
||||
else
|
||||
return ArgumentType.STRING;
|
||||
}
|
||||
}
|
||||
|
|
52
lib/argparse/Parser.dart
Normal file
52
lib/argparse/Parser.dart
Normal file
|
@ -0,0 +1,52 @@
|
|||
import 'package:libac_dart/argparse/Args.dart';
|
||||
import 'package:libac_dart/argparse/types/Bool.dart';
|
||||
import 'package:libac_dart/argparse/types/Integers.dart';
|
||||
import 'package:libac_dart/argparse/types/String.dart';
|
||||
|
||||
class ArgumentParser {
|
||||
/// Parses a list of arguments.
|
||||
///
|
||||
/// [args] The list of arguments to parse.
|
||||
/// Returns an Arguments object representing the input.
|
||||
static Arguments parse(List<String> args) {
|
||||
final ret = Arguments();
|
||||
for (var i = 0; i < args.length; i++) {
|
||||
final arg = args[i];
|
||||
if (arg.startsWith('--')) {
|
||||
var key = arg.substring(2); // Remove the '--' part of the argument
|
||||
dynamic value;
|
||||
|
||||
// Check if the argument has a value attached (either --arg=value or --arg value)
|
||||
if (i + 1 < args.length && !args[i + 1].startsWith('--')) {
|
||||
value = args[i + 1]; // --arg value
|
||||
i++; // Skip the next argument as it is the value
|
||||
} else if (arg.contains('=')) {
|
||||
value = arg.substring(arg.indexOf('=') + 1); // --arg=value
|
||||
key = key.substring(0, key.indexOf('='));
|
||||
}
|
||||
|
||||
// Determine the argument type and add it to the list
|
||||
if (int.tryParse(value?.toString() ?? '') != null) {
|
||||
ret.setArg(
|
||||
IntegerArgument(name: key, value: int.parse(value.toString())));
|
||||
} else if (value?.toString().toLowerCase() == 'true' ||
|
||||
value?.toString().toLowerCase() == 'false' ||
|
||||
value == null) {
|
||||
if (value != null)
|
||||
ret.setArg(BoolArgument(
|
||||
name: key, value: value.toString().toLowerCase() == 'true'));
|
||||
else
|
||||
ret.setArg(BoolArgument(name: key, value: true));
|
||||
} else if (double.tryParse(value?.toString() ?? '') != null) {
|
||||
ret.setArg(
|
||||
DoubleArgument(name: key, value: double.parse(value.toString())));
|
||||
} else {
|
||||
// Default to StringArgument if no matching type is found
|
||||
ret.setArg(StringArgument(name: key, value: value?.toString() ?? ''));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
}
|
|
@ -2,9 +2,13 @@ import 'package:libac_dart/argparse/Args.dart';
|
|||
|
||||
class BoolArgument extends Argument<bool> {
|
||||
bool _value = false;
|
||||
BoolArgument({required super.name, super.description = ""}) {
|
||||
hasValue = false;
|
||||
_value = true;
|
||||
bool _hasValue = true;
|
||||
|
||||
BoolArgument({required super.name, bool? value = true}) {
|
||||
if (value != null)
|
||||
this._value = value;
|
||||
else
|
||||
_hasValue = false;
|
||||
}
|
||||
|
||||
@override
|
||||
|
@ -21,4 +25,9 @@ class BoolArgument extends Argument<bool> {
|
|||
String toString() {
|
||||
return "BooleanArgument{ ${name}=${_value} }";
|
||||
}
|
||||
|
||||
@override
|
||||
bool hasValue() {
|
||||
return _hasValue;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,16 +1,25 @@
|
|||
import 'package:libac_dart/argparse/Args.dart';
|
||||
|
||||
class IntegerArgument extends Argument<int> {
|
||||
final int value;
|
||||
int _value = 0;
|
||||
bool _hasValue = false;
|
||||
|
||||
IntegerArgument(
|
||||
{required super.name, required this.value, super.description = ""}) {
|
||||
hasValue = value != 0;
|
||||
IntegerArgument({required super.name, int? value}) {
|
||||
if (value != null) {
|
||||
this._value = value;
|
||||
_hasValue = true;
|
||||
} else
|
||||
_hasValue = false;
|
||||
}
|
||||
|
||||
@override
|
||||
bool hasValue() {
|
||||
return _hasValue;
|
||||
}
|
||||
|
||||
@override
|
||||
int getValue() {
|
||||
return value;
|
||||
return _value;
|
||||
}
|
||||
|
||||
@override
|
||||
|
@ -20,16 +29,25 @@ class IntegerArgument extends Argument<int> {
|
|||
|
||||
@override
|
||||
String toString() {
|
||||
return "IntegerArgument{ ${name}=${value} }";
|
||||
return "IntegerArgument{ ${name}=${_value} }";
|
||||
}
|
||||
}
|
||||
|
||||
class DoubleArgument extends Argument<double> {
|
||||
final double value;
|
||||
double _value = 0.0;
|
||||
bool _hasValue = false;
|
||||
|
||||
DoubleArgument(
|
||||
{required super.name, required this.value, super.description = ""}) {
|
||||
hasValue = value != 0.0;
|
||||
DoubleArgument({required super.name, double? value}) {
|
||||
if (value != null) {
|
||||
_hasValue = true;
|
||||
_value = value;
|
||||
} else
|
||||
_hasValue = false;
|
||||
}
|
||||
|
||||
@override
|
||||
bool hasValue() {
|
||||
return _hasValue;
|
||||
}
|
||||
|
||||
@override
|
||||
|
@ -39,11 +57,11 @@ class DoubleArgument extends Argument<double> {
|
|||
|
||||
@override
|
||||
double getValue() {
|
||||
return value;
|
||||
return _value;
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return "DoubleArgument{ ${name}=${value} }";
|
||||
return "DoubleArgument{ ${name}=${_value} }";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,16 +1,25 @@
|
|||
import 'package:libac_dart/argparse/Args.dart';
|
||||
|
||||
class StringArgument extends Argument<String> {
|
||||
final String value;
|
||||
String _value = "";
|
||||
bool _hasValue = false;
|
||||
|
||||
StringArgument(
|
||||
{required super.name, required this.value, super.description}) {
|
||||
hasValue = value.isNotEmpty;
|
||||
StringArgument({required super.name, String? value}) {
|
||||
if (value != null) {
|
||||
_hasValue = true;
|
||||
_value = value;
|
||||
} else
|
||||
_hasValue = false;
|
||||
}
|
||||
|
||||
@override
|
||||
bool hasValue() {
|
||||
return _hasValue;
|
||||
}
|
||||
|
||||
@override
|
||||
String getValue() {
|
||||
return value;
|
||||
return _value;
|
||||
}
|
||||
|
||||
@override
|
||||
|
@ -20,6 +29,6 @@ class StringArgument extends Argument<String> {
|
|||
|
||||
@override
|
||||
String toString() {
|
||||
return "StringArgument{ ${name}=${value} }";
|
||||
return "StringArgument{ ${name}=${_value} }";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
class Constants {
|
||||
static const VERSION = "1.3.012225+0304";
|
||||
static const VERSION = "1.4.012225+0357";
|
||||
static const NBT_REVISION = "1.3.012225+0304";
|
||||
static const ARGS_REVISION = "1.4.012225+0357";
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue