Cliffy

Parse options

Flags

By default the parseFlags method parses all flags and tries to autodetect the type. With the flags options you can specify an Array of flags options. If the flags option is set the parseFlags method will throw an error for all unknown or invalid flags. You can find a list of all possible flag options here.

Parse

With the parse method you can add a custom handler for handling and parsing types.

i

❗ The parse method will be called for all types, which means it overrides also all build in types!

import {
  ArgumentValue,
  parseFlags,
} from "https://deno.land/x/cliffy@v0.25.7/flags/mod.ts";

parseFlags(Deno.args, {
  flags: [{
    name: "foo",
    type: "float",
  }],
  parse: ({ label, name, value, type }: ArgumentValue) => {
    switch (type) {
      case "float":
        if (isNaN(Number(value))) {
          throw new Error(
            `${label} "${name}" must be of type "${type}", but got "${value}".`,
          );
        }
        return parseFloat(value);
      default:
        throw new Error(`Unknown type "${type}".`);
    }
  },
});Copy
$ deno run https://deno.land/x/cliffy@v0.25.7/examples/flags/custom_option_processing.ts --foo 1.2
{ flags: { foo: 1.2 }, unknown: [], literal: [] }

$ deno run https://deno.land/x/cliffy@v0.25.7/examples/flags/custom_option_processing.ts --foo abc
error: Uncaught Error: Option "--foo" must be of type "float", but got "abc".Copy

Option callback

The option callback method option is called for each parsed option.

Stop early

If stopEarly is enabled, all values starting from the first non option argument will be added to the unknown array (can be combined with stopOnUnknown).

Stop on unknown

If stopOnUnknown is enabled, all values starting from the first unknown option argument will be added to the unknown array (can be combined with stopEarly).

Allow empty

If an required option is specified, by default an error is thrown if the command is invoked without any flags. To disable this behavior you can set allowEmpty to true. The default is false.

Dotted

By default, all option names that have dots in their names are converted to nested objects. For example, { "foo.bar": 1 } becomes { "foo": { "bar": 1 } }. You can disable this behavior by setting the dotted option to false. This is required when parsing command line arguments in multiple steps (see parse-context).