Skip to content

feat(cli): support pattern generation options passed on command line#2291

Open
markscott-ms wants to merge 3 commits intofinos:mainfrom
markscott-ms:feat/1951-generate-with-options
Open

feat(cli): support pattern generation options passed on command line#2291
markscott-ms wants to merge 3 commits intofinos:mainfrom
markscott-ms:feat/1951-generate-with-options

Conversation

@markscott-ms
Copy link
Copy Markdown
Contributor

Description

Resolves #1951 opened by @niamhg-ms

This pull request adds support for providing pre-defined option choices to the CLI's generate command, allowing users to skip interactive prompts by specifying choices via a JSON array or file. It also includes robust tests and error handling for this new feature.

New CLI feature: Pre-defined option choices

  • Added a new --option-choices flag to the generate command, allowing users to provide option choices as a JSON array of descriptions or as a path to a JSON file. When this flag is used, the CLI skips interactive prompts and uses the provided choices. [1] [2] [3]

Implementation and logic

  • Implemented the loadChoicesFromInput function in generate-options.ts to parse and validate pre-defined choices, ensuring they match valid options in the pattern and providing clear error messages for invalid input. [1] [2]

Testing

  • Added comprehensive unit tests for loadChoicesFromInput, covering scenarios for inline JSON, JSON files, empty arrays, invalid choices, non-array input, and malformed JSON. [1] [2] [3]
  • Added a CLI-level test to verify that providing --option-choices correctly skips interactive prompts and uses the specified choices.

Type of Change

  • 🐛 Bug fix (non-breaking change which fixes an issue)
  • ✨ New feature (non-breaking change which adds functionality)
  • 💥 Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • 📚 Documentation update
  • 🎨 Code style/formatting changes
  • ♻️ Refactoring (no functional changes)
  • ⚡ Performance improvements
  • ✅ Test additions or updates
  • 🔧 Chore (maintenance, dependencies, CI, etc.)

Affected Components

  • CLI (cli/)
  • Schema (calm/)
  • CALM AI (calm-ai/)
  • CALM Hub (calm-hub/)
  • CALM Hub UI (calm-hub-ui/)
  • CALM Server (calm-server/)
  • CALM Widgets (calm-widgets/)
  • Documentation (docs/)
  • Shared (shared/)
  • VS Code Extension (calm-plugins/vscode/)
  • Dependencies
  • CI/CD

Commit Message Format ✅

Testing

  • I have tested my changes locally
  • I have added/updated unit tests
  • All existing tests pass

Checklist

  • My commits follow the conventional commit format
  • I have updated documentation if necessary
  • I have added tests for my changes (if applicable)
  • My changes follow the project's coding standards

Copy link
Copy Markdown
Member

@jpgough-ms jpgough-ms left a comment

Choose a reason for hiding this comment

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

I'm cool with the code for taking in a JSON file, but I do wonder if a better way for the CLI input would be to:

File Based
calm generate -p sample.pattern.json --option-choices file.json

Pairs of options and choices
calm generate -p sample.pattern.json --option http-or-https --choice https

cc/ @willosborne

@markscott-ms
Copy link
Copy Markdown
Contributor Author

I'm cool with the code for taking in a JSON file, but I do wonder if a better way for the CLI input would be to:

File Based calm generate -p sample.pattern.json --option-choices file.json

Pairs of options and choices calm generate -p sample.pattern.json --option http-or-https --choice https

cc/ @willosborne

Agree on the providing unique-id+choice, don't like how the syntax would look (and not sure if Commander can cope) with multiple options+choices.

@jpgough-ms
Copy link
Copy Markdown
Member

@markscott-ms it looks like the way to do this is... inline json. Seems a bit nasty, but maybe that's just the way it is

jpgough-ms
jpgough-ms previously approved these changes Mar 31, 2026
@willosborne
Copy link
Copy Markdown
Member

willosborne commented Apr 2, 2026

Agreed that it would be nice to specify the answers inline without needing a separate file. I think @jpgough-ms suggestion of using commander arguments for this is probably quite difficult given the annotation-based way that commander parses these things but i think we can get something almost as nice with commander's built in features.

I wonder if we could get something more like the way Kubectl lets you specify e.g. labels:

calm generate -p sample.pattern.json --option http-or-https=https potato-or-potato=potato

Working spike of that:

const {program} = require('commander')
program.
    option('-o, --options <options...>')
    .action((opts) => {
        console.log(opts.options)
    })

program.parse(process.argv)
$ node commander-test.js --options abc=123 def=ghi
[ 'abc=123', 'def=ghi' ]

This works fine because we don't use arguments, only options. so there's no issue with knowing when the options list ends and the arguments begin (normally this would need --)

Another thought is that is all-or-nothing, you can either have it load answers for every question or ask every question.
Would be nice to get answers from the file for all provided and then fall back to asking the user for anything left over.
i.e. we pre-fill the platform opinions but leave some things open to the user.
We can add that if it's needed though :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add CLI option for using calm generate with pre-defined options

4 participants