EdgeAIG's picture
download
raw
6.22 kB
/**
* Global flags for Effect CLI command trees. Global flags are parsed outside a
* single command's local flags and can apply to a command and its descendants.
*
* This module defines two kinds of global flags: action flags, which run an
* effect and stop normal command execution, and setting flags, which provide a
* parsed value to the command handler through the Effect context. It also
* defines the built-in help, version, shell-completion, and log-level flags
* used by `Command.run` and `Command.runWith`.
*
* @since 4.0.0
*/
import * as Console from "../../Console.js";
import * as Context from "../../Context.js";
import * as Effect from "../../Effect.js";
import * as Option from "../../Option.js";
import * as CliOutput from "./CliOutput.js";
import * as Completions_ from "./Completions.js";
import * as Flag from "./Flag.js";
import * as CommandDescriptor from "./internal/completions/descriptor.js";
import * as HelpInternal from "./internal/help.js";
/* ========================================================================== */
/* Constructors */
/* ========================================================================== */
/**
* Creates an Action flag that performs a side effect and exits.
*
* @category constructors
* @since 4.0.0
*/
export const action = options => ({
_tag: "Action",
flag: options.flag,
run: options.run
});
/**
* Creates a Setting flag that configures the command handler's environment.
*
* @category constructors
* @since 4.0.0
*/
export const setting = id => options => {
settingIdCounter += 1;
const ref = Context.Service(`effect/unstable/cli/GlobalFlag/${id}/${settingIdCounter}`);
return Object.assign(ref, {
_tag: "Setting",
id,
flag: options.flag
});
};
let settingIdCounter = 0;
/* ========================================================================== */
/* Built-in Flag References */
/* ========================================================================== */
/**
* Defines the `--help` / `-h` global flag, which shows help documentation for the
* active command path.
*
* @see {@link BuiltIns} for the default list containing this flag
* @see {@link action} for defining custom action global flags
*
* @category references
* @since 4.0.0
*/
export const Help = /*#__PURE__*/action({
flag: /*#__PURE__*/Flag.boolean("help").pipe(/*#__PURE__*/Flag.withAlias("h"), /*#__PURE__*/Flag.withDescription("Show help information")),
run: (_, {
command,
commandPath
}) => Effect.gen(function* () {
const formatter = yield* CliOutput.Formatter;
const helpDoc = yield* HelpInternal.getHelpForCommandPath(command, commandPath, BuiltIns);
yield* Console.log(formatter.formatHelpDoc(helpDoc));
})
});
/**
* Defines the global action flag for showing command version information.
*
* **When to use**
*
* Use to add a built-in `--version / -v` flag to a command runner.
*
* @category references
* @since 4.0.0
*/
export const Version = /*#__PURE__*/action({
flag: /*#__PURE__*/Flag.boolean("version").pipe(/*#__PURE__*/Flag.withAlias("v"), /*#__PURE__*/Flag.withDescription("Show version information")),
run: (_, {
command,
version
}) => Effect.gen(function* () {
const formatter = yield* CliOutput.Formatter;
yield* Console.log(formatter.formatVersion(command.name, version));
})
});
/**
* Defines the `--completions` global flag, which prints a shell completion script for
* the given shell.
*
* **Details**
*
* Accepted values are `bash`, `zsh`, `fish`, and `sh`; `sh` is normalized to
* `bash`.
*
* @category references
* @since 4.0.0
*/
export const Completions = /*#__PURE__*/action({
flag: /*#__PURE__*/Flag.choice("completions", ["bash", "zsh", "fish", "sh"]).pipe(Flag.optional, /*#__PURE__*/Flag.map(v => Option.map(v, s => s === "sh" ? "bash" : s)), /*#__PURE__*/Flag.withMetavar("<bash|zsh|fish|sh>"), /*#__PURE__*/Flag.withDescription("Print shell completion script")),
run: (shell, {
command
}) => Effect.gen(function* () {
if (Option.isNone(shell)) return;
const descriptor = CommandDescriptor.fromCommand(command);
yield* Console.log(Completions_.generate(command.name, shell.value, descriptor));
})
});
/**
* Defines the global setting flag for command log level.
*
* **When to use**
*
* Use to add a built-in `--log-level` option that configures the minimum log
* level for the command.
*
* @category references
* @since 4.0.0
*/
export const LogLevel = /*#__PURE__*/setting("log-level")({
flag: /*#__PURE__*/Flag.choiceWithValue("log-level", [["all", "All"], ["trace", "Trace"], ["debug", "Debug"], ["info", "Info"], ["warn", "Warn"], ["warning", "Warn"], ["error", "Error"], ["fatal", "Fatal"], ["none", "None"]]).pipe(Flag.optional, /*#__PURE__*/Flag.withDescription("Sets the minimum log level"), /*#__PURE__*/Flag.withMetavar("<all|trace|debug|info|warn|warning|error|fatal|none>"))
});
/* ========================================================================== */
/* References */
/* ========================================================================== */
/**
* Built-in global flags in default precedence order.
*
* **When to use**
*
* Use when extending or inspecting the default global-flag set that
* `Command.runWith` prepends before user-defined global flags.
*
* **Details**
*
* The built-ins are `Help`, `Version`, `Completions`, and `LogLevel`.
* `Command.runWith` prepends these built-ins when collecting and parsing global
* flags.
*
* **Gotchas**
*
* Action flags are processed in active flag order and the first present action
* exits, so this array controls built-in action precedence.
*
* @see {@link Help} for the help action flag
* @see {@link Version} for the version action flag
* @see {@link Completions} for the shell-completions action flag
* @see {@link LogLevel} for the built-in log-level setting flag
*
* @category references
* @since 4.0.0
*/
export const BuiltIns = [Help, Version, Completions, LogLevel];
//# sourceMappingURL=GlobalFlag.js.map

Xet Storage Details

Size:
6.22 kB
·
Xet hash:
c41dffb55cd65ed3c72264b367fdb6434a944cc502354df75c8dc5191a3faeef

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.