22// for details. All rights reserved. Use of this source code is governed by a
33// BSD-style license that can be found in the LICENSE file.
44
5+ import '../args_wrapper.dart' ;
56import 'package:pub_semver/pub_semver.dart' ;
67import 'package:yaml/yaml.dart' ;
78
@@ -36,44 +37,47 @@ class AddCommand extends PubCommand {
3637 @override
3738 String get docUrl => 'https://dart.dev/tools/pub/cmd/pub-add' ;
3839 @override
39- bool get isOffline => argResults['offline' ];
40-
41- bool get isDev => argResults['dev' ];
42- bool get isDryRun => argResults['dry-run' ];
43- String get gitUrl => argResults['git-url' ];
44- String get gitPath => argResults['git-path' ];
45- String get gitRef => argResults['git-ref' ];
46- String get hostUrl => argResults['hosted-url' ];
47- String get path => argResults['path' ];
48- String get sdk => argResults['sdk' ];
40+ bool get isOffline => argResults.get (isOfflineFlag);
41+
42+ final Flag isOfflineFlag = Flag ('offline' ,
43+ help: 'Use cached packages instead of accessing the network.' );
44+ final Flag isDev = Flag ('dev' ,
45+ abbr: 'd' ,
46+ negatable: false ,
47+ help: 'Adds package to the development dependencies instead.' );
48+ final Flag isDryRun = Flag ('dry-run' ,
49+ help: 'Use cached packages instead of accessing the network.' ,
50+ negatable: false );
51+
52+ final Flag precompile = Flag ('precompile' ,
53+ help: 'Precompile executables in immediate dependencies.' );
54+
55+ final Option gitUrl = Option ('git-url' , help: 'Git URL of the package' );
56+ final Option gitRef =
57+ Option ('git-ref' , help: 'Git branch or commit to be retrieved' );
58+ final Option gitPath =
59+ Option ('git-path' , help: 'Path of git package in repository' );
60+ final Option hostUrl =
61+ Option ('hosted-url' , help: 'URL of package host server' );
62+ final Option path = Option ('path' , help: 'Local path' );
63+ final Option sdk = Option ('sdk' , help: 'SDK source for package' );
4964
5065 bool get hasGitOptions => gitUrl != null || gitRef != null || gitPath != null ;
5166 bool get hasHostOptions => hostUrl != null ;
5267
5368 AddCommand () {
54- argParser.addFlag ('dev' ,
55- abbr: 'd' ,
56- negatable: false ,
57- help: 'Adds package to the development dependencies instead.' );
58-
59- argParser.addOption ('git-url' , help: 'Git URL of the package' );
60- argParser.addOption ('git-ref' ,
61- help: 'Git branch or commit to be retrieved' );
62- argParser.addOption ('git-path' , help: 'Path of git package in repository' );
63- argParser.addOption ('hosted-url' , help: 'URL of package host server' );
64- argParser.addOption ('path' , help: 'Local path' );
65- argParser.addOption ('sdk' , help: 'SDK source for package' );
66-
67- argParser.addFlag ('offline' ,
68- help: 'Use cached packages instead of accessing the network.' );
69-
70- argParser.addFlag ('dry-run' ,
71- abbr: 'n' ,
72- negatable: false ,
73- help: "Report what dependencies would change but don't change any." );
74-
75- argParser.addFlag ('precompile' ,
76- help: 'Precompile executables in immediate dependencies.' );
69+ argParser.add (isDev);
70+ argParser.add (gitUrl);
71+ argParser.add (gitRef);
72+ argParser.add (gitPath);
73+ argParser.add (hostUrl);
74+ argParser.add (sdk);
75+
76+ argParser.add (isOfflineFlag);
77+
78+ argParser.add (isDryRun);
79+
80+ argParser.add (precompile);
7781 }
7882
7983 @override
@@ -126,7 +130,7 @@ class AddCommand extends PubCommand {
126130 'does not satisfy constraint "${package .constraint }".' );
127131 }
128132
129- if (isDryRun) {
133+ if (argResults. get ( isDryRun) ) {
130134 /// Even if it is a dry run, run `acquireDependencies` so that the user
131135 /// gets a report on the other packages that might change version due
132136 /// to this new dependency.
@@ -138,19 +142,19 @@ class AddCommand extends PubCommand {
138142 .acquireDependencies (
139143 SolveType .GET ,
140144 dryRun: true ,
141- precompile: argResults[ ' precompile' ] ,
145+ precompile: argResults. get ( precompile) ,
142146 );
143147 } else {
144148 /// Update the `pubspec.yaml` before calling [acquireDependencies] to
145149 /// ensure that the modification timestamp on `pubspec.lock` and
146150 /// `.dart_tool/package_config.json` is newer than `pubspec.yaml` ,
147151 /// ensuring that [entrypoint.assertUptoDate] will pass.
148- _updatePubspec (resultPackage, packageInformation, isDev);
152+ _updatePubspec (resultPackage, packageInformation, argResults. get ( isDev) );
149153
150154 /// Create a new [Entrypoint] since we have to reprocess the updated
151155 /// pubspec file.
152156 await Entrypoint .current (cache).acquireDependencies (SolveType .GET ,
153- precompile: argResults[ ' precompile' ] );
157+ precompile: argResults. get ( precompile) );
154158 }
155159
156160 if (isOffline) {
@@ -172,7 +176,7 @@ class AddCommand extends PubCommand {
172176 final devDependencyNames =
173177 devDependencies.map ((devDependency) => devDependency.name);
174178
175- if (isDev) {
179+ if (argResults. get ( isDev) ) {
176180 /// TODO(walnut): Change the error message once pub upgrade --bump is
177181 /// released
178182 if (devDependencyNames.contains (package.name)) {
@@ -252,18 +256,19 @@ class AddCommand extends PubCommand {
252256 ArgumentError .checkNotNull (package, 'package' );
253257
254258 final _conflictingFlagSets = [
255- ['git-url' , 'git-ref' , 'git-path' ],
256- ['hosted-url' ],
257- [' path' ],
258- [' sdk' ],
259+ [gitUrl, gitRef, gitPath ],
260+ [hostUrl ],
261+ [path],
262+ [sdk],
259263 ];
260264
261- for (final flag
262- in _conflictingFlagSets.expand ((s) => s).where (argResults.wasParsed)) {
265+ for (final flag in _conflictingFlagSets
266+ .expand ((s) => s)
267+ .where (argResults.argWasParsed)) {
263268 final conflictingFlag = _conflictingFlagSets
264269 .where ((s) => ! s.contains (flag))
265270 .expand ((s) => s)
266- .firstWhere (argResults.wasParsed , orElse: () => null );
271+ .firstWhere (argResults.argWasParsed , orElse: () => null );
267272 if (conflictingFlag != null ) {
268273 usageException (
269274 'Packages can only have one source, "pub add" flags "--$flag " and '
0 commit comments