Skip to content

Commit

Permalink
Add short flags to CLI
Browse files Browse the repository at this point in the history
Add short flags to CLI.
  • Loading branch information
hachibu committed Mar 17, 2024
1 parent 5a458c6 commit b491c4e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ output formats (e.g. json, yaml).

- Install [Deno](https://docs.deno.com/runtime/manual/#install-deno)
- Run `deno task install`
- Run `datelta --start "Jan 1, 1990" | jq`
- Run `datelta -b "Jan 1, 1990" | jq`

## Usage

Time between start date and end date.

```
datelta --start "jan 10, 1990 12:30:01" --end "feb 2002" | jq
datelta -b "jan 10, 1990 12:30:01" -e "feb 2002" | jq
```

```json
Expand All @@ -32,7 +32,7 @@ datelta --start "jan 10, 1990 12:30:01" --end "feb 2002" | jq
Time since start date.

```
datelta --start "mar 2006" | jq
datelta -b "mar 2006" | jq
```

```json
Expand Down
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"tasks": {
"dev": "deno run --watch src/main.ts",
"dev": "deno run src/main.ts",
"install": "deno install -f --name datelta src/main.ts",
"uninstall": "deno uninstall datelta"
}
Expand Down
36 changes: 21 additions & 15 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,39 @@ const help = `datelta ${version}
Datelta calculates the difference between 2 dates with millisecond precision.
Options:
--start Start date (default: now).
--end End date (default: now).
--fmt Output format (default: json, options: json, yaml).
-b, --beg Beginning date (default: now).
-e, --end Ending date (default: now).
-f, --fmt Output format (default: json, options: json, yaml).
-h, --help Print help.
-V, --version Print version.`;

function main() {
const allowedFmts = ["json", "yaml"];
const flags = parseArgs(Deno.args, {
boolean: ["help", "h", "version", "V"],
string: ["start", "end", "fmt"],
string: ["beg", "b", "end", "e", "fmt", "f"],
});
const allowedFmts = ["json", "yaml"];

if (flags.help || flags.h) {
flags.help ??= flags.h;
flags.version ??= flags.V;
flags.beg ??= flags.b;
flags.end ??= flags.e;
flags.fmt ??= flags.f;

if (flags.help) {
return console.log(help);
} else if (flags.version || flags.V) {
} else if (flags.version) {
return console.log(version);
}

let startDate = new Date();
let begDate = new Date();
let endDate = new Date();

if (flags.start && flags.end) {
startDate = new Date(flags.start);
if (flags.beg && flags.end) {
begDate = new Date(flags.beg);
endDate = new Date(flags.end);
} else if (flags.start) {
startDate = new Date(flags.start);
} else if (flags.beg) {
begDate = new Date(flags.beg);
} else {
return console.log(help);
}
Expand All @@ -43,11 +49,11 @@ function main() {
if (!allowedFmts.includes(fmt)) {
return console.error(
`invalid argument: fmt was "${fmt}" expected one of:`,
allowedFmts,
allowedFmts
);
}

const dd = { ...dateDiff(startDate, endDate) };
const dd = { ...dateDiff(begDate, endDate) };

if (fmt === "json") {
console.log(JSON.stringify(dd));
Expand All @@ -56,7 +62,7 @@ function main() {
} else {
return console.error(
`invalid argument: fmt was "${fmt}" expected one of:`,
allowedFmts,
allowedFmts
);
}
}
Expand Down

0 comments on commit b491c4e

Please sign in to comment.