Commit 024da5c8 authored by king011's avatar king011

wrapper args

parent bb0fead5
# Files and directories created by pub
.dart_tool/
.packages
# Remove the following pattern if you wish to check in your lock file
pubspec.lock
# Conventional directory for build outputs
build/
# Directory created by dartdoc
doc/api/
## 1.0.0
- Initial version, created by Stagehand
A library for Dart command-line.
Parses raw command-line arguments into a set of options and values. Wrapper package:args/args.dart for my style
![](images/args.png)
## Usage
A simple usage example:
```dart
import 'package:wrapper_args/wrapper_args.dart';
main(List<String> arguments) {
// create root command
final root = _root();
// create subcommand
root.addCommand([
_server(),
_client(),
]);
// parse and execute
root.parseAndExecute(arguments);
}
Command _root() {
final parser = ArgParser()
..addFlag(
"help",
abbr: "h",
negatable: false,
help: "Print this usage information",
)
..addFlag(
"version",
abbr: "v",
negatable: false,
help: "Print the version",
);
return Command(
help: "Usage: wrapper_args [<subcommand>] [<options>]",
parser: parser,
execute: (command, results) {
if (results["help"]) {
print("${command.usage}");
} else if (results["version"]) {
print("version 1.0.0");
}
},
);
}
Command _server() {
final parser = ArgParser()
..addFlag(
"help",
abbr: "h",
negatable: false,
help: "Print this usage information",
)
..addOption(
"listen",
abbr: "l",
help: "tcp listen address",
);
return Command(
name: "server",
help: "simulator server",
parser: parser,
execute: (command, results) {
if (results["help"]) {
print(command.usage);
} else {
print("server work at ${results['listen']}");
}
},
);
}
Command _client() {
final parser = ArgParser()
..addFlag(
"help",
abbr: "h",
negatable: false,
help: "Print this usage information",
)
..addOption(
"target",
abbr: "t",
help: "connect target",
);
return Command(
name: "client",
help: "simulator client",
parser: parser,
execute: (command, results) {
if (results["help"]) {
print(command.usage);
} else {
print("connet ${results['target']} ...");
}
},
);
}
```
# Defines a default set of lint rules enforced for
# projects at Google. For details and rationale,
# see https://github.com/dart-lang/pedantic#enabled-lints.
include: package:pedantic/analysis_options.yaml
# For lint rules and documentation, see http://dart-lang.github.io/linter/lints.
# Uncomment to specify additional rules.
# linter:
# rules:
# - camel_case_types
analyzer:
# exclude:
# - path/to/excluded/files/**
import 'package:wrapper_args/wrapper_args.dart';
main(List<String> arguments) {
// create root command
final root = _root();
// create subcommand
root.addCommand([
_server(),
_client(),
]);
// parse and execute
root.parseAndExecute(arguments);
}
Command _root() {
final parser = ArgParser()
..addFlag(
"help",
abbr: "h",
negatable: false,
help: "Print this usage information",
)
..addFlag(
"version",
abbr: "v",
negatable: false,
help: "Print the version",
);
return Command(
help: "Usage: wrapper_args [<subcommand>] [<options>]",
parser: parser,
execute: (command, results) {
if (results["help"]) {
print("${command.usage}");
} else if (results["version"]) {
print("version 1.0.0");
}
},
);
}
Command _server() {
final parser = ArgParser()
..addFlag(
"help",
abbr: "h",
negatable: false,
help: "Print this usage information",
)
..addOption(
"listen",
abbr: "l",
help: "tcp listen address",
);
return Command(
name: "server",
help: "simulator server",
parser: parser,
execute: (command, results) {
if (results["help"]) {
print(command.usage);
} else {
print("server work at ${results['listen']}");
}
},
);
}
Command _client() {
final parser = ArgParser()
..addFlag(
"help",
abbr: "h",
negatable: false,
help: "Print this usage information",
)
..addOption(
"target",
abbr: "t",
help: "connect target",
);
return Command(
name: "client",
help: "simulator client",
parser: parser,
execute: (command, results) {
if (results["help"]) {
print(command.usage);
} else {
print("connet ${results['target']} ...");
}
},
);
}
import 'package:args/args.dart';
import 'dart:core';
class Command {
final Map<String, Command> _keys;
final String name;
final ArgParser parser;
final String help;
final String longHelp;
void Function(Command, ArgResults) execute;
Command({
this.name,
this.help = "",
this.longHelp = "",
this.parser,
this.execute,
}) : assert(name != null),
assert(help != null),
assert(longHelp != null),
assert(parser != null),
assert(execute != null),
_keys = Map<String, Command>();
addCommand(List<Command> cmd) {
assert(cmd != null);
cmd.forEach((cmd) {
assert(cmd != null);
final name = cmd.name;
assert(name != null);
if (_keys.containsKey(name)) {
throw AssertionError("subcommand $name already exists");
}
_keys[name] = cmd;
parser.addCommand(name, cmd.parser);
});
}
parseAndExecute(List<String> arguments) {
final results = parser.parse(arguments);
if (results.command == null) {
execute(this, results);
} else {
final name = results.command.name;
final executable = _keys[name];
if (executable == null) {
throw UnsupportedError("not found subcommand : $name");
} else {
executable.execute(executable, results.command);
}
}
}
String _help() {
if (longHelp != null && longHelp.isNotEmpty) {
return longHelp;
}
return help;
}
String _usage;
String get usage {
if (_usage != null) {
return _usage;
}
final buffer = StringBuffer();
final help = _help();
if (help != null && help.isNotEmpty) {
if (help.endsWith("\n")) {
buffer.write(help);
} else {
buffer.write(help + "\n");
}
}
buffer.write('''\nOptions:
${parser.usage}
''');
if (_keys.isNotEmpty) {
buffer.write("\nSubcommands:\n");
_keys.forEach((name, command) {
buffer.write("$name\t\t${command.help}\n");
});
}
_usage = buffer.toString();
return _usage;
}
}
/// Parses raw command-line arguments into a set of options and values.
///
/// Wrapper package:args/args.dart for my style
library wrapper_args;
export 'package:args/args.dart';
export './src/command.dart';
name: wrapper_args
description: Parses raw command-line arguments into a set of options and values. Wrapper package:args/args.dart for my style
version: 1.0.0
homepage: https://gitlab.com/king011/wrapper_args
author: zuiwuchang <zuiwuchang@gmail.com>
environment:
sdk: '>=2.1.0 <3.0.0'
dependencies:
args: ^1.5.1
dev_dependencies:
pedantic: ^1.0.0
test: ^1.0.0
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment