-
Notifications
You must be signed in to change notification settings - Fork 77
[cli_config] First version #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
414a8fb
7ec934d
af82a32
07ca71b
8244bbe
0c43865
95c1bc8
a1e1251
b2361de
5702466
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,4 +6,4 @@ | |
| /// and environment variables. | ||
| library cli_config; | ||
|
|
||
| export 'src/cli_config.dart'; | ||
| export 'src/config.dart'; | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| // Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file | ||
| // for details. All rights reserved. Use of this source code is governed by a | ||
| // BSD-style license that can be found in the LICENSE file. | ||
|
|
||
| import 'package:args/args.dart'; | ||
|
|
||
| class CliParser { | ||
| final ArgParser parser = () { | ||
| final parser = ArgParser(); | ||
| parser.addFlag( | ||
| 'help', | ||
| abbr: 'h', | ||
| help: 'Show this help.', | ||
| ); | ||
| parser.addMultiOption( | ||
| 'define', | ||
| abbr: 'D', | ||
| help: '''Define or override a config property from command line. | ||
| The same option can be passed multiple times. | ||
| Keys should only contain lower-case alphanumeric characters, underscores, | ||
| and '.'s''', | ||
| ); | ||
| parser.addOption( | ||
| 'config', | ||
| abbr: 'c', | ||
| help: '''Path to JSON or YAML config file. | ||
| Keys should only contain lower-case alphanumeric characters, and underscores. | ||
| Hierarchies should be maps.''', | ||
| ); | ||
| return parser; | ||
| }(); | ||
|
|
||
| ArgResults parse(List<String> args) => parser.parse(args); | ||
| } | ||
|
|
||
| class DefinesParser { | ||
| Map<String, List<String>> parse(List<String> args) { | ||
| final regex = RegExp('([a-z_.]+)=(.+)'); | ||
| final defines = <String, List<String>>{}; | ||
| for (final arg in args) { | ||
| final match = regex.matchAsPrefix(arg); | ||
| if (match == null || match.group(0) != arg) { | ||
| throw FormatException("Define '$arg' does not match expected pattern " | ||
| "'${regex.pattern}'."); | ||
| } | ||
| final key = match.group(1)!; | ||
| final value = match.group(2)!; | ||
| defines[key] = (defines[key] ?? [])..add(value); | ||
| } | ||
| return defines; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| // Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file | ||
| // for details. All rights reserved. Use of this source code is governed by a | ||
| // BSD-style license that can be found in the LICENSE file. | ||
|
|
||
| import 'config.dart'; | ||
| import 'provider.dart'; | ||
|
|
||
| class CliProvider extends Provider { | ||
| /// Configuration options passed in via CLI arguments. | ||
| /// | ||
| /// Options can be passed multiple times, so the values here are a list. | ||
| /// | ||
| /// Stored as a flat non-hierarchical structure, keys contain `.`. | ||
| final Map<String, List<String>> _cli; | ||
|
|
||
| CliProvider(this._cli); | ||
|
|
||
| @override | ||
| String? getOptionalString(String key) { | ||
|
||
| final value = _cli[key]; | ||
| if (value == null) { | ||
| return null; | ||
| } | ||
| if (value.length > 1) { | ||
| throw FormatException( | ||
| "More than one value was passed for '$key' in the CLI defines." | ||
| ' Values passed: $value'); | ||
| } | ||
| return value.single; | ||
| } | ||
|
|
||
| @override | ||
| List<String>? getOptionalStringList( | ||
| String key, { | ||
| String? splitPattern, | ||
| }) { | ||
| final cliValue = _cli[key]; | ||
| if (cliValue == null) { | ||
| return null; | ||
| } | ||
| if (splitPattern != null) { | ||
| return [for (final value in cliValue) ...value.split(splitPattern)]; | ||
| } | ||
| return cliValue; | ||
| } | ||
|
|
||
| @override | ||
| bool? getOptionalBool(String key) { | ||
| final stringValue = getOptionalString(key); | ||
| if (stringValue != null) { | ||
| Provider.throwIfUnexpectedValue( | ||
| key, stringValue, Config.boolStrings.keys); | ||
| return Config.boolStrings[stringValue]!; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| @override | ||
| Uri? getOptionalPath( | ||
| String key, { | ||
| bool resolveUri = false, | ||
| }) { | ||
| assert(resolveUri == false); | ||
| final stringValue = getOptionalString(key); | ||
| if (stringValue != null) { | ||
| return Provider.fileSystemPathToUri(stringValue); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| @override | ||
| List<Uri>? getOptionalPathList( | ||
| String key, { | ||
| String? splitPattern, | ||
| bool resolveUri = false, | ||
| }) { | ||
| assert(resolveUri == false); | ||
| final strings = getOptionalStringList(key, splitPattern: splitPattern); | ||
| return strings?.map((e) => Uri(path: e)).toList(); | ||
| } | ||
|
|
||
| @override | ||
| String toString() => 'CliProvider($_cli)'; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.