args_simple 1.3.0
args_simple: ^1.3.0 copied to clipboard
a simple argument parser and handler, integrated with JSON and dart:io.
args_simple #
A simple argument parser and handler, integrated with JSON and dart:io.
API Documentation #
See the API Documentation for a full list of functions, classes and extension.
Usage #
import 'dart:io';
import 'package:args_simple/args_simple_io.dart';
/// Examples of valid values for the parameter `args`:
///
/// - `/path/config.json --port 81 -verbose`
/// - `/path/config.json -verbose --sys-config /my/sys-config-dir`
/// - `/path/config.json root`
void main(List<String> argsOrig) {
var args = ArgsSimple.parse(argsOrig);
if (args.isEmpty) {
print(
'USAGE: [%configFile.json] [%user] [--port] [--sys-config %systemConfig] [-verbose]');
exit(0);
}
// Argument #0 is a JSON `File`:
var config = args.argumentMatches(0, RegExp(r'.json$'))
? args.argumentAsFileContentJSON(0)!
: {};
// Argument #1 is an optional `String`, with `guest` as default:
var user = args.argumentAsString(1, 'guest');
// Option `sys-config` is a `Directory`, with `/default/sys-config-dir` as default:
var systemConfigDir = args.optionAsDirectory(
'sys-config', Directory('/default/sys-config-dir'))!;
// Option `--port` is an `int`, with 8080 as default:
var port = args.optionAsInt('port', 8080);
// Check for flag `-verbose`:
var verbose = args.flag('verbose');
if (verbose) {
print('-- Config: $config');
print('-- User: $user');
print('-- System-Config Dir: $systemConfigDir');
print('-- Port: $port');
print('-- Verbose: $verbose');
print(args);
}
}
Parsing rules #
ArgsSimple.parse reads each element of the command line as:
| Input | Parsed as |
|---|---|
-- |
End of options: every following element is an argument, even if it starts with -. |
--key=value |
Option key = value (split on the first =; the value may be empty or contain =). |
--key value |
Option key = value. The next element is always the value (a trailing --key gets ''). |
-Dkey=value, -Pkey=value |
Property key = value (Java-style; split on the first = or :). |
-name |
Flag name (including -Debug/-Port: -D/-P without =/: is a flag). |
-1, -1.5, -1e3 |
Argument (a negative number). |
| anything else | Argument. |
- An option given more than once is stored as a
Listof its values. --flag=trueis an option, not a flag: read it withoptionAsBool(for exampleargs.flagOr('verbose', null) ?? args.optionAsBool('verbose') ?? false).- Keys are normalized by
ArgsSimple.normalizeKey: lower-cased, keeping only Unicode letters, marks and digits. So--sys-config,--sys_configand--SysConfigare the same option, and--Ünïcodeisünïcode. toList()gives a command line thatparsereads back to the same values (arguments that start with-are emitted after--), andtoJson()/fromJson()round-trip throughjsonEncode/jsonDecode.
Source #
The official source code is hosted @ GitHub:
Features and bugs #
Please file feature requests and bugs at the issue tracker.
Contribution #
Any help from the open-source community is always welcome and needed:
- Found an issue?
- Please fill a bug report with details.
- Wish a feature?
- Open a feature request with use cases.
- Are you using and liking the project?
- Promote the project: create an article, do a post or make a donation.
- Are you a developer?
- Fix a bug and send a pull request.
- Implement a new feature, like other training algorithms and activation functions.
- Improve the Unit Tests.
- Have you already helped in any way?
- Many thanks from me, the contributors and everybody that uses this project!
If you donate 1 hour of your time, you can contribute a lot, because others will do the same, just be part and start with your 1 hour.
Author #
Graciliano M. Passos: gmpassos@GitHub.