-
Notifications
You must be signed in to change notification settings - Fork 783
Add -help-json flag for machine-readable CLI help
#7198
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
Open
ewels
wants to merge
12
commits into
master
Choose a base branch
from
help-json-cli
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a2329e0
Add `-help-json` flag for machine-readable CLI help
ewels c2a3b18
Document `-help-json` CLI flag
ewels 5faaca1
Merge branch 'master' into help-json-cli
pditommaso e075f44
Apply suggestions from code review
ewels 09b41a3
Show hidden options in -help-json, flagged with hidden:true
ewels dbd475f
Make -help-json recursive for subcommands
ewels 426985e
Show -help-json tip on all sub-command help screens
ewels 884aefc
Make -help-json root a recursive index and condense JSON output
ewels 05bebd7
Widen -help-json line budget to 160 cols and refresh docs
ewels c258e0f
Merge branch 'master' into help-json-cli
ewels 63f99ed
Carry -help-json through the plugin command after #7197
ewels 61155a9
Merge branch 'master' into help-json-cli
ewels File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
237 changes: 237 additions & 0 deletions
237
modules/nextflow/src/main/groovy/nextflow/cli/CliSchema.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,237 @@ | ||
| /* | ||
| * Copyright 2013-2026, Seqera Labs | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package nextflow.cli | ||
|
|
||
| import java.lang.reflect.Field | ||
|
|
||
| import com.beust.jcommander.DynamicParameter | ||
| import com.beust.jcommander.Parameter | ||
| import com.beust.jcommander.Parameters | ||
| import groovy.json.JsonOutput | ||
| import groovy.transform.CompileDynamic | ||
| import groovy.transform.CompileStatic | ||
|
|
||
| /** | ||
| * Build a machine-readable (JSON) description of the Nextflow command line | ||
| * interface by introspecting the JCommander {@link Parameter} / {@link Parameters} | ||
| * annotations. | ||
| * | ||
| * This powers the global {@code -help-json} flag, which lets tools and LLMs | ||
| * discover the CLI one command at a time without scraping the rendered | ||
| * {@code -help} text: | ||
| * | ||
| * <pre> | ||
| * nextflow -help-json # global options + index of all commands | ||
| * nextflow run -help-json # full option/argument detail for `run` | ||
| * </pre> | ||
| * | ||
| * @author Phil Ewels <phil.ewels@seqera.io> | ||
| */ | ||
| @CompileStatic | ||
| class CliSchema { | ||
|
|
||
| /** | ||
| * Build the schema for the top-level program: the global options plus a | ||
| * name + description index of every available command. | ||
| */ | ||
| static String root(CliOptions options, List<CmdBase> commands) { | ||
| final schema = [ | ||
| name : 'nextflow', | ||
| path : 'nextflow', | ||
| usage : 'nextflow [options] COMMAND [arg...]', | ||
| params : paramsOf(CliOptions), | ||
| subcommands : commandIndex(commands), | ||
| ] | ||
| return render(schema) | ||
| } | ||
|
|
||
| /** | ||
| * Build the schema for a single command: its usage, options and positional | ||
| * arguments. | ||
| */ | ||
| static String command(CmdBase cmd) { | ||
| final clazz = cmd.getClass() | ||
| final params = paramsOf(clazz) | ||
| final schema = [ | ||
| name : cmd.name, | ||
| path : "nextflow ${cmd.name}".toString(), | ||
| usage : usageOf(cmd, params), | ||
| params : params, | ||
| ] as Map<String,Object> | ||
|
|
||
| final description = descriptionOf(clazz) | ||
| if( description ) | ||
| schema.help = description | ||
| final aliases = aliasesOf(clazz) | ||
| if( aliases ) | ||
| schema.aliases = aliases | ||
|
|
||
| return render(schema) | ||
| } | ||
|
|
||
| /** | ||
| * Map each command name to its description and aliases. Nextflow commands | ||
| * are flat (no nested groups), so a single index lists them all; drill into | ||
| * any one with {@code nextflow <command> -help-json}. | ||
| */ | ||
| @CompileDynamic | ||
| private static Map<String,Object> commandIndex(List<CmdBase> commands) { | ||
| final index = new TreeMap<String,Object>() | ||
| for( CmdBase cmd : commands ) { | ||
| final clazz = cmd.getClass() | ||
| final description = descriptionOf(clazz) | ||
| // only advertise commands that opt in with a description, matching `-help` | ||
| if( !description ) | ||
| continue | ||
| final entry = [help: description] as Map<String,Object> | ||
| final aliases = aliasesOf(clazz) | ||
| if( aliases ) | ||
| entry.aliases = aliases | ||
| index[cmd.name] = entry | ||
| } | ||
| return index | ||
| } | ||
|
|
||
| /** | ||
| * Introspect the {@link Parameter} / {@link DynamicParameter} annotations of | ||
| * a command (or options) class into a list of JSON-friendly maps. Inherited | ||
| * fields are included; hidden and meta options ({@code -h}, {@code -help}, | ||
| * {@code -help-json}) are skipped. | ||
| */ | ||
| @CompileDynamic | ||
| private static List<Map<String,Object>> paramsOf(Class clazz) { | ||
| final instance = newInstance(clazz) | ||
| final result = [] | ||
| for( Field field : declaredFields(clazz) ) { | ||
| final p = field.getAnnotation(Parameter) | ||
| final dp = field.getAnnotation(DynamicParameter) | ||
| if( p ) | ||
| addParam(result, field, p.names() as List<String>, p.description(), p.hidden(), p.required(), p.arity(), instance) | ||
| else if( dp ) | ||
| addParam(result, field, dp.names() as List<String>, dp.description(), dp.hidden(), false, -1, instance) | ||
| } | ||
| // sort for a stable, deterministic ordering: reflection field order is not | ||
| // guaranteed by the JVM, but this is a machine-readable contract | ||
| result.sort { it.opts ? it.opts[0] : it.name } | ||
| return result | ||
| } | ||
|
|
||
| @CompileDynamic | ||
| private static void addParam(List result, Field field, List<String> names, String description, boolean hidden, boolean required, int arity, Object instance) { | ||
| if( hidden ) | ||
| return | ||
| if( names.any { it in META_OPTIONS } ) | ||
| return | ||
|
|
||
| final isArgument = !names | ||
| final isFlag = field.getType() == boolean || field.getType() == Boolean || arity == 0 | ||
| final entry = [ | ||
| name : field.getName(), | ||
| kind : isArgument ? 'argument' : 'option', | ||
| type : typeName(field), | ||
| ] as Map<String,Object> | ||
| if( names ) | ||
| entry.opts = names | ||
| if( description ) | ||
| entry.help = description | ||
| if( required ) | ||
| entry.required = true | ||
| if( isFlag ) | ||
| entry.is_flag = true | ||
|
|
||
| final defValue = defaultOf(field, instance) | ||
| if( defValue != null && !isFlag ) | ||
| entry.default = defValue | ||
|
|
||
| result << entry | ||
| } | ||
|
|
||
| /** Build a usage string, e.g. {@code nextflow run [options] <args>}. */ | ||
| private static String usageOf(CmdBase cmd, List<Map<String,Object>> params) { | ||
| final pieces = ["nextflow ${cmd.name}".toString()] | ||
| if( params.any { it.kind == 'option' } ) | ||
| pieces << '[options]' | ||
| if( params.any { it.kind == 'argument' } ) | ||
| pieces << '[args...]' | ||
| return pieces.join(' ') | ||
| } | ||
|
|
||
| @CompileDynamic | ||
| private static String descriptionOf(Class clazz) { | ||
| return clazz.getAnnotation(Parameters)?.commandDescription() ?: null | ||
| } | ||
|
|
||
| @CompileDynamic | ||
| private static List<String> aliasesOf(Class clazz) { | ||
| final names = clazz.getAnnotation(Parameters)?.commandNames() | ||
| return names ? (names as List<String>) : null | ||
| } | ||
|
|
||
| private static String typeName(Field field) { | ||
| return field.getType().getSimpleName() | ||
| } | ||
|
|
||
| /** Read a field's default value from a fresh instance, ignoring empty values. */ | ||
| @CompileDynamic | ||
| private static Object defaultOf(Field field, Object instance) { | ||
| if( instance == null ) | ||
| return null | ||
| try { | ||
| field.setAccessible(true) | ||
| final value = field.get(instance) | ||
| if( value == null || value == false ) | ||
| return null | ||
| if( value instanceof Collection && value.isEmpty() ) | ||
| return null | ||
| if( value instanceof Map && value.isEmpty() ) | ||
| return null | ||
| return value instanceof CharSequence || value instanceof Number || value instanceof Boolean | ||
| ? value | ||
| : value.toString() | ||
| } | ||
| catch( Exception e ) { | ||
| return null | ||
| } | ||
| } | ||
|
|
||
| private static Object newInstance(Class clazz) { | ||
| try { | ||
| return clazz.getDeclaredConstructor().newInstance() | ||
| } | ||
| catch( Exception e ) { | ||
| return null | ||
| } | ||
| } | ||
|
|
||
| /** Collect declared fields from the class and its superclasses. */ | ||
| private static List<Field> declaredFields(Class clazz) { | ||
| final fields = new ArrayList<Field>() | ||
| Class current = clazz | ||
| while( current != null && current != Object ) { | ||
| fields.addAll(current.getDeclaredFields()) | ||
| current = current.getSuperclass() | ||
| } | ||
| return fields | ||
| } | ||
|
|
||
| private static String render(Map schema) { | ||
| return JsonOutput.prettyPrint(JsonOutput.toJson(schema)) | ||
| } | ||
|
|
||
| private static final List<String> META_OPTIONS = ['-h', '-help', '-help-json'] | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.