Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@

A simple, intuitive library for building CLI applications in D.

**[API Reference Documentation](https://modpm.github.io/cmd)**
[**API Reference Documentation**](https://modpm.github.io/cmd)

## Features

- **Nested subcommands** — Supports hierarchical command structures, allowing commands to contain other commands.
- **Flexible arguments** — Required (`<>`) and optional (`[]`) arguments with variadic support (`...`).
- **Rich option handling** — Supports `--option=value` and `--option value` forms.
- **Rich option handling** — Supports `--option=value`, `--option value`, and grouped short flags such as `-abc`
(equivalent to `-a -b -c`).
- **Automatic help generation** — Built-in help and usage text generation.
- **Array collection** — Repeated options are automatically collected as arrays.
- **Type-safe parsing** — Clean API for accessing parsed arguments and options.
Expand Down
20 changes: 18 additions & 2 deletions src/cmd/command.d
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public class Command {
public int printHelp() const {
auto doc = new Document();
doc.add("Usage:".bold(), usage(true));

if (descriptionStr !is null)
doc.add("Description:".bold(), descriptionStr);

Expand Down Expand Up @@ -330,6 +330,22 @@ public class Command {
if (program.helpOption() !is null && program.helpOption().matches(arg))
exit(printHelp());

if (arg.length > 2 && arg[0] == '-' && arg[1] != '-') {
foreach (c; arg[1 .. $]) {
const shortArg = "-" ~ c;
const(Flag) f = findFlag(shortArg);
if (f !is null) {
parsedArgs.setFlag(f);
continue;
}
const(Option) opt = findOption(shortArg);
if (opt !is null)
error("missing value for option '" ~ shortArg ~ "'", 2);
program.error("unknown option '" ~ shortArg ~ "'", 2);
}
continue;
}

const(Flag) flag = findFlag(arg);
if (flag !is null) {
parsedArgs.setFlag(flag);
Expand All @@ -342,7 +358,7 @@ public class Command {
if (option !is null) {
string value;
if (equalsIndex >= 0)
value =arg[equalsIndex + 1..$];
value = arg[equalsIndex + 1..$];
else if (i + 1 < args.length)
value = args[++i];
else
Expand Down