diff --git a/example/analysis_options.yaml b/example/analysis_options.yaml index dc3fca1..4924fdb 100644 --- a/example/analysis_options.yaml +++ b/example/analysis_options.yaml @@ -1,5 +1,9 @@ include: package:lints/recommended.yaml +analyzer: + enable-experiment: + - inline-class + linter: rules: prefer_relative_imports: true diff --git a/example/lib/runner.dart b/example/lib/runner.dart index 24ad915..b49cdad 100644 --- a/example/lib/runner.dart +++ b/example/lib/runner.dart @@ -16,14 +16,11 @@ class GitRunner extends _$GitRunner { /// The name of the branch to be merged into the current branch. required String branch, - // /// Pass merge strategy specific option through to the merge strategy. + /// Pass merge strategy specific option through to the merge strategy. MergeStrategy strategy = MergeStrategy.ort, /// Perform the merge and commit the result. - @Flag() bool? commit, - @Option(hide: true, parser: specialDurationParser) Duration? uri, - @MultiOption(allowed: [1, 2, 3]) int? ff, - Set someSet = const {1, 2, 3}, + bool? commit, }) async { print('Merging branch $branch'); if (commit == true) { @@ -34,7 +31,3 @@ class GitRunner extends _$GitRunner { } enum MergeStrategy { ort, recursive, resolve, octopus, ours, subtree } - -Duration specialDurationParser(String input) { - return Duration(seconds: int.parse(input)); -} diff --git a/example/lib/runner.g.dart b/example/lib/runner.g.dart index a76b1fb..0474dd4 100644 --- a/example/lib/runner.g.dart +++ b/example/lib/runner.g.dart @@ -25,9 +25,6 @@ class MergeCommand extends Command { required String branch, MergeStrategy strategy, bool? commit, - Duration? uri, - int? ff, - Set someSet, }) userMethod; @override @@ -55,29 +52,7 @@ class MergeCommand extends Command { 'subtree', ], ) - ..addFlag('commit') - ..addOption( - 'uri', - hide: true, - mandatory: false, - ) - ..addOption( - 'ff', - mandatory: false, - allowed: [ - '1', - '2', - '3', - ], - ) - ..addMultiOption( - 'some-set', - defaultsTo: [ - '1', - '2', - '3', - ], - ); + ..addFlag('commit'); @override Future run() { @@ -88,12 +63,6 @@ class MergeCommand extends Command { ? EnumParser(MergeStrategy.values).parse(results['strategy']) : MergeStrategy.ort, commit: results['commit'] != null ? results['commit'] : null, - uri: - results['uri'] != null ? specialDurationParser(results['uri']) : null, - ff: results['ff'] != null ? int.parse(results['ff']) : null, - someSet: results['some-set'] != null - ? List.from(results['some-set']).map(int.parse).toSet() - : const {1, 2, 3}, ); } } diff --git a/example/lib/stash.dart b/example/lib/stash.dart index c7afdc5..978f8bd 100644 --- a/example/lib/stash.dart +++ b/example/lib/stash.dart @@ -13,7 +13,7 @@ class StashSubcommand extends _$StashSubcommand { @cliCommand Future push( String message, { - @Option(help: 'Whether to include untracked files.', abbr: 'u') + /// Whether to include untracked files. bool includeUntracked = false, }) async { print('Stashing changes with message: $message'); diff --git a/example/lib/stash.g.dart b/example/lib/stash.g.dart index 9381207..b810936 100644 --- a/example/lib/stash.g.dart +++ b/example/lib/stash.g.dart @@ -43,8 +43,6 @@ class PushCommand extends Command { ) ..addFlag( 'include-untracked', - abbr: 'u', - help: 'Whether to include untracked files.', defaultsTo: false, ); diff --git a/src/cli_gen/lib/src/analysis/parameters/type_parser_expression_builder.dart b/src/cli_gen/lib/src/analysis/parameters/type_parser_expression_builder.dart index 0cd9f33..d130ca1 100644 --- a/src/cli_gen/lib/src/analysis/parameters/type_parser_expression_builder.dart +++ b/src/cli_gen/lib/src/analysis/parameters/type_parser_expression_builder.dart @@ -91,16 +91,17 @@ class TypeParserExpressionBuilder { // if the element is a extension type, get the extension type erasure // to get the underlying type, then get the corresponding parser // by calling `getParserForParameter` recursively. - final isExtensionType = paramType.extensionTypeErasure != paramType; - if (isExtensionType) { - // final erasure = type.extensionTypeErasure; - // return getParserForParameter(element, erasure); - throw InvalidGenerationSource( - 'Extension types are not supported by `cli-gen` yet.', - element: element, - todo: 'Please use a supported type', - ); - } + // final isExtensionType = paramType.extensionTypeErasure != paramType; + // if (isExtensionType) { + // // example: if `FooExtensionType` has an actualType of `int`, + // // then the parser would be `FooExtensionType.parse`. + + // // throw InvalidGenerationSource( + // // 'Extension types are not supported by `cli-gen` yet.', + // // element: element, + // // todo: 'Please use a supported type', + // // ); + // } throw InvalidGenerationSource( '`${paramType.element!.name}` is not a supported type for arg parsing.', diff --git a/src/cli_gen/lib/src/analysis/utils/get_parameter_ast_node.dart b/src/cli_gen/lib/src/analysis/utils/get_parameter_ast_node.dart new file mode 100644 index 0000000..4dee7f3 --- /dev/null +++ b/src/cli_gen/lib/src/analysis/utils/get_parameter_ast_node.dart @@ -0,0 +1,18 @@ +import 'package:analyzer/dart/ast/ast.dart'; +import 'package:analyzer/dart/element/element.dart'; +import 'package:build/build.dart'; +import 'package:source_gen/source_gen.dart'; + +extension ParameterAstNodeX on ParameterElement { + Future asAstNode(Resolver resolver) async { + final result = await resolver.astNodeFor(this); + if (result is! FormalParameter) { + throw InvalidGenerationSourceError( + 'Expected a FormalParameter, but got $result', + element: this, + ); + } + + return result; + } +}