1// Copyright 2017 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5import 'package:args/args.dart'; 6 7import 'context.dart'; 8 9/// command-line flags and options that were specified during the invocation of 10/// the Flutter tool. 11Flags get flags => context.get<Flags>(); 12 13/// Encapsulation of the command-line flags and options that were specified 14/// during the invocation of the Flutter tool. 15/// 16/// An instance of this class is set into the [AppContext] upon invocation of 17/// the Flutter tool (immediately after the arguments have been parsed in 18/// [FlutterCommandRunner]) and is available via the [flags] global property. 19class Flags { 20 Flags(this._globalResults) 21 : assert(_globalResults != null); 22 23 final ArgResults _globalResults; 24 25 /// Gets the value of the specified command-line flag/option that was set 26 /// during the invocation of the Flutter tool. 27 /// 28 /// This will first search for flags that are specific to the command and will 29 /// fall back to global flags. 30 /// 31 /// If a flag has a default value and the user did not explicitly specify a 32 /// value on the command-line, this will return the default value. 33 /// 34 /// If the specified flag is not defined or was not specified and had no 35 /// default, then this will return `null`. 36 dynamic operator [](String key) { 37 final ArgResults commandResults = _globalResults.command; 38 final Iterable<String> options = commandResults?.options; 39 if (options != null && options.contains(key)) 40 return commandResults[key]; 41 else if (_globalResults.options.contains(key)) 42 return _globalResults[key]; 43 return null; 44 } 45 46 /// `true` iff the given flag/option was either explicitly specified by the 47 /// user at the command-line or it was defined to have a default value. 48 bool contains(String key) { 49 final ArgResults commandResults = _globalResults.command; 50 final Iterable<String> options = commandResults?.options; 51 return (options != null && options.contains(key)) || _globalResults.options.contains(key); 52 } 53} 54 55class EmptyFlags implements Flags { 56 const EmptyFlags(); 57 58 @override 59 ArgResults get _globalResults => null; 60 61 @override 62 String operator [](String key) => null; 63 64 @override 65 bool contains(String key) => false; 66} 67