Skip to content
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

Cliffy should allow to pass option with empty value if option is declared as optional. #731

Open
scandinave opened this issue Aug 27, 2024 · 1 comment

Comments

@scandinave
Copy link

scandinave commented Aug 27, 2024

Hi,
In my use case i have an option defined like this:

  .option('-v, --version <number:semver>',  'The version part of the tag <repo>/<image>:<platform>-<version>-<qualifer>')

This option is called inside a CI pipeline like this:

myCli command --version '${params.RELEASE_VERSION}'

params.RELEASE_VERSION can be a string or empty depending of the user that trigger the pipeline. Currently, Cliffy throw an exception error: Missing value for option "--version". This forces me to due parsing to only add --version if RELEASE_VERSION is not empty.

I expect that an optional option can also be specifying with empty value.

@fritogotlayed
Copy link

fritogotlayed commented Feb 27, 2025

I have a similar issue where at least allowing a empty string to be provided to an option would be useful. In my case a empty string in the option is significantly different than the omission of the option. It can be though of similar to a nullable boolean. Right now I have to include a second option and make them conflict with one another as a work around. Granted my use case is around collapsing multiple .env and .env.xyz files into a .envrc with a slightly different format.

Example (can also be seen here):

import { Command, ArgumentValue } from '@cliffy/command';

function nullableBooleanType({ label, name, value }: ArgumentValue): boolean | undefined {
  if (value === 'true' || value === '1') {
    return true;
  }
  if (value === 'false' || value === '0') {
    return false;
  }
  return undefined;
}

export const writeEnv = new Command()
  .name('sample')
  .description('example')
  .type('nullableBoolean', nullableBooleanType)
  .option(
    '-d, --dir <dir:string>',
    '...',
  )
  .option(
    '-e, --env <env:string>',
    '...',
  )
  // NOTE: If we can figure out how to use -e "" and be valid that would remove the need for the baseEnv option
  //       and we can just use -e "" to indicate that we want to use the base .env file
  //       Look into the `value` callback on the above option.
  .option(
    '--base-env',
    '...',
    {default: false, conflicts: ['env']},
  )
  .option(
    '--include-local <includeLocal:nullableBoolean>',
    '...',
    {default: undefined},
  )
  .action(async ({ dir, env, baseEnv, includeLocal }) => { /* implementation */ });

Having the ability to provide a empty string and undefined to the env argument would help reduce complexity. I would love to know if there is a way to support this or if I'm stuck with the work around I have in place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants