Skip to content

Commit dcccd83

Browse files
committed
first commit
0 parents  commit dcccd83

File tree

10 files changed

+199
-0
lines changed

10 files changed

+199
-0
lines changed

.gitignore

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Files and directories created by pub
2+
.dart_tool/
3+
.packages
4+
# Remove the following pattern if you wish to check in your lock file
5+
pubspec.lock
6+
7+
# Conventional directory for build outputs
8+
build/
9+
10+
# Directory created by dartdoc
11+
doc/api/
12+
13+
**.iml
14+
.idea/

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## 1.0.0
2+
3+
- Initial version, created by Stagehand

LICENSE

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Copyright 2014, the Dart project authors. All rights reserved.
2+
Redistribution and use in source and binary forms, with or without
3+
modification, are permitted provided that the following conditions are
4+
met:
5+
6+
* Redistributions of source code must retain the above copyright
7+
notice, this list of conditions and the following disclaimer.
8+
* Redistributions in binary form must reproduce the above
9+
copyright notice, this list of conditions and the following
10+
disclaimer in the documentation and/or other materials provided
11+
with the distribution.
12+
* Neither the name of Google Inc. nor the names of its
13+
contributors may be used to endorse or promote products derived
14+
from this software without specific prior written permission.
15+
16+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
XArgs is a simple library to resolve command arguments
2+
3+
## Usage
4+
5+
A simple usage example:
6+
7+
```dart
8+
import 'package:xargs/xargs.dart';
9+
10+
main() {
11+
String command = '1 2 3 4 -mode run --enable -who John Denis Peter';
12+
List<String> argArray = command.split(RegExp('\\s+'));
13+
XArgs xArgs = XArgs.of(argArray);
14+
print(xArgs); // ["1","2","3","4",{"key":"mode","values":["run"]},{"key":"enable","values":[]},{"key":"who","values":["John","Denis","Peter"]}]
15+
print(xArgs.valuesNoKey()); // [1, 2, 3, 4]
16+
print(xArgs.valueNoKeyAt(0)); // 1
17+
print(xArgs['mode']); // run
18+
print(xArgs.hasKey('enable')); // true
19+
print(xArgs['who']); // John
20+
print(xArgs.firstValue('who')); // John
21+
print(xArgs.values('who')); // [John, Denis, Peter]
22+
}
23+
24+
```

analysis_options.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Defines a default set of lint rules enforced for
2+
# projects at Google. For details and rationale,
3+
# see https://github.com/dart-lang/pedantic#enabled-lints.
4+
include: package:pedantic/analysis_options.yaml
5+
6+
# For lint rules and documentation, see http://dart-lang.github.io/linter/lints.
7+
# Uncomment to specify additional rules.
8+
# linter:
9+
# rules:
10+
# - camel_case_types
11+
12+
analyzer:
13+
# exclude:
14+
# - path/to/excluded/files/**

example/xargs_example.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import 'package:xargs/xargs.dart';
2+
3+
main() {
4+
String command = '1 2 3 4 -mode run --enable -who John Denis Peter';
5+
List<String> argArray = command.split(RegExp('\\s+'));
6+
XArgs xArgs = XArgs.of(argArray);
7+
print(xArgs); // ["1","2","3","4",{"key":"mode","values":["run"]},{"key":"enable","values":[]},{"key":"who","values":["John","Denis","Peter"]}]
8+
print(xArgs.valuesNoKey()); // [1, 2, 3, 4]
9+
print(xArgs.valueNoKeyAt(0)); // 1
10+
print(xArgs['mode']); // run
11+
print(xArgs.hasKey('enable')); // true
12+
print(xArgs['who']); // John
13+
print(xArgs.firstValue('who')); // John
14+
print(xArgs.values('who')); // [John, Denis, Peter]
15+
}

lib/src/xargs_core.dart

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import 'dart:convert';
2+
3+
RegExp _regExp = RegExp('^(-{1,2})(.+)\$');
4+
5+
class XArgs {
6+
List<_XArg> _args = [];
7+
8+
XArgs._();
9+
10+
XArgs.of(List<String> argArray) {
11+
_args.clear();
12+
for (var value in argArray) {
13+
if (_regExp.hasMatch(value)) break;
14+
_args.add(_XArg.ofSingleValue(value));
15+
}
16+
17+
_XArg target;
18+
for (var value in argArray) {
19+
if (_regExp.hasMatch(value)) {
20+
target = _XArg._();
21+
target.key = (_regExp.allMatches(value).toList()[0]).group(2);
22+
_args.add(target);
23+
} else {
24+
target?.values?.add(value);
25+
}
26+
}
27+
}
28+
29+
bool hasKey(String key) => _args.any((arg) => arg.key == key);
30+
31+
List<String> values(String key) {
32+
var found = _args.where((arg) => arg.key == key);
33+
return found.isNotEmpty ? found.first.values : [];
34+
}
35+
36+
List<String> valuesNoKey() {
37+
return _args.where((arg) => arg.key.isEmpty).map((arg) => arg.values.single).toList();
38+
}
39+
40+
String valueNoKeyAt(int index) {
41+
var values = valuesNoKey();
42+
return values.isEmpty || index > values.length ? null : values[index];
43+
}
44+
45+
String firstValue(String key) {
46+
var found = values(key);
47+
return found.isNotEmpty ? found.first : null;
48+
}
49+
50+
String operator [](String key) {
51+
return firstValue(key);
52+
}
53+
54+
@override
55+
String toString() {
56+
return jsonEncode(_args.map((o) => o.isSingleValue ? o.values.single : {'key': o.key, 'values': o.values}).toList());
57+
}
58+
}
59+
60+
class _XArg {
61+
String key;
62+
List<String> values = [];
63+
bool get isSingleValue => values.isNotEmpty && key.isEmpty && values.length == 1;
64+
65+
_XArg._();
66+
67+
_XArg.ofKeyValues(List<String> elements) {
68+
key = _regExp.allMatches(elements[0]).toList()[0].group(2);
69+
values = elements.skip(1).toList();
70+
}
71+
72+
_XArg.ofSingleValue(String value) {
73+
key = '';
74+
values.add(value);
75+
}
76+
77+
@override
78+
String toString() {
79+
return 'Arg{key: $key, values: $values}';
80+
}
81+
}

lib/xargs.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
library xargs;
2+
3+
export 'src/xargs_core.dart';

pubspec.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: xargs
2+
description: XArgs is a simple library to resolve command arguments
3+
version: 1.0.0
4+
homepage: https://github.com/Arxing/dart-xargs
5+
author: Arxing <[email protected]>
6+
7+
environment:
8+
sdk: '>=2.4.0 <3.0.0'
9+
10+
#dependencies:
11+
# path: ^1.6.0
12+
13+
dev_dependencies:
14+
pedantic: ^1.7.0
15+
test: ^1.6.0

test/xargs_test.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
void main() {
3+
4+
}

0 commit comments

Comments
 (0)