-
Notifications
You must be signed in to change notification settings - Fork 23
Add support for emitting usage as a JSON schema #897
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
base: main
Are you sure you want to change the base?
Changes from all commits
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,6 +6,7 @@ import 'dart:collection'; | |
|
||
import 'allow_anything_parser.dart'; | ||
import 'arg_results.dart'; | ||
import 'json_schema.dart'; | ||
import 'option.dart'; | ||
import 'parser.dart'; | ||
import 'usage.dart'; | ||
|
@@ -392,4 +393,48 @@ class ArgParser { | |
/// Finds the option whose name or alias matches [name], or `null` if no | ||
/// option has that name or alias. | ||
Option? findByNameOrAlias(String name) => options[_aliases[name] ?? name]; | ||
|
||
Map<String, Object?> get jsonSchema { | ||
final properties = <String, Schema>{}; | ||
final required = <String>[]; | ||
for (final option in _options.values) { | ||
if (option.hide) continue; | ||
var help = option.help; | ||
final extras = <String>[]; | ||
if (option.defaultsTo != null) { | ||
extras.add('defaults to "${option.defaultsTo}"'); | ||
} | ||
if (option.allowed?.isNotEmpty ?? false) { | ||
extras.add('allowed values: ${option.allowed?.join(', ')}'); | ||
} | ||
if (extras.isNotEmpty) { | ||
help = [ | ||
if (help != null) help, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can use |
||
...extras, | ||
].join('\n'); | ||
} | ||
final schema = switch (option.type) { | ||
OptionType.flag => Schema.bool( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd just use They're great for discovery, but this schema code here is completely internal, so we don't need that. |
||
description: help, | ||
), | ||
OptionType.single => Schema.string( | ||
description: help, | ||
), | ||
OptionType.multiple => Schema.list( | ||
description: help, | ||
items: Schema.string(), | ||
), | ||
_ => throw StateError('Unhandled Option Type: ${option.type.name}') | ||
}; | ||
|
||
if (option.mandatory) { | ||
required.add(option.name); | ||
} | ||
properties[option.name] = schema; | ||
} | ||
return Schema.object( | ||
properties: properties, | ||
required: required, | ||
).asMap(); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So basically just: var properties = <String, Object?>{};
var required = [];
for (var option in _options.values) {
if (option.hide) continue;
var Option(:name, :type, :help) = option;
final description = [?help,
if (option.defaultsTo != null) 'defaults to "${option.defaultsTo}"'
if (option.allowed?.isNotEmpty ?? false)
'allowed values: ${option.allowed?.join(', ')}'
].join('\n');
properties[name] = {
...switch (type) {
OptionType.bool => const { 'type': 'boolean' },
OptionType.single => const { 'type': 'string' } ,
OptionType.multi => const {
'type': 'list',
'items': {'type': 'string'},
},
}
if (description.isNotEmpty) 'description': description,
};
if (option.mandatory) required.add(name);
}
return {
'type': 'object',
'properties': properties,
if (required.isNotEmpty) 'required': required
};
} I'm not sure a complete JSONSchema implementation is worth it, especially one we didn't write ourselves, but is vendoring and now have to maintain anyway. |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Documentation.
Why is this here? What can it be used for? By whom, and where?