Skip to content

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkgs/args/lib/src/allow_anything_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ class AllowAnythingParser implements ArgParser {
@override
int? get usageLineLength => null;

@override
Map<String, Object?> get jsonSchema =>
const {'type': 'object', 'properties': {}};

@override
ArgParser addCommand(String name, [ArgParser? parser]) {
throw UnsupportedError(
Expand Down
45 changes: 45 additions & 0 deletions pkgs/args/lib/src/arg_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 {
Copy link
Member

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?

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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can use ?help now?

...extras,
].join('\n');
}
final schema = switch (option.type) {
OptionType.flag => Schema.bool(
Copy link
Member

@lrhn lrhn Jun 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd just use BoolSchema directly, and probably drop the redirecting factories.

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();
}
Copy link
Member

@lrhn lrhn Jun 17, 2025

Choose a reason for hiding this comment

The 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.

}
Loading
Loading