Skip to content

Commit

Permalink
update: examples
Browse files Browse the repository at this point in the history
  • Loading branch information
nktknshn committed Jun 13, 2023
1 parent af0bcea commit 86b64c8
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 24 deletions.
14 changes: 8 additions & 6 deletions examples/complex/main.ts → examples/complex/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,23 @@ import {
import * as client from "./client";
import * as server from "./server";

const cmd = comp(
const cliCommand = comp(
_ => _.option("debug", { alias: "d", type: "boolean", default: false }),
subs(["client", "c"], "client management", client.cmd),
subs(["server", "s"], "server management", server.cmd),
);

const handler = createHandlerFor(cmd, {
const handler = createHandlerFor(cliCommand, {
"client": client.handler,
"server": server.handler,
});

async function main() {
const { yargs, result } = buildAndParse(cmd);
const { yargs, result } = buildAndParse(
cliCommand,
process.argv.slice(2),
_ => _.scriptName("complex-cli"),
);

if (Either.isLeft(result)) {
failClient(yargs, result);
Expand All @@ -34,9 +38,7 @@ async function main() {
);
}

const f = handler.handle(result.right);

await f({ yargs });
await handler.handle(result.right)({ yargs });
}

main().catch(console.error);
11 changes: 10 additions & 1 deletion examples/complex/client/client/args.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { comm, comp } from "../../../../src";
import { parseAddress } from "../../common";
import { defaultConfigFile } from "../config/args";

export const commandList = comm(
"list <address> [path]",
Expand Down Expand Up @@ -42,7 +43,15 @@ export const commandUpload = comm(
);

export const cmd = comp(
_ => _.option("debug", { alias: "d", type: "boolean", default: false }),
_ =>
_.options({
"debug": { alias: "d", type: "boolean", default: false },
"configFile": {
alias: "c",
type: "string",
default: defaultConfigFile,
},
}),
commandList,
commandDownload,
commandUpload,
Expand Down
8 changes: 8 additions & 0 deletions examples/complex/client/config/args.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { comm, comp } from "../../../../src";

export const defaultConfigFile = "config.json";

export const cmd = comp(
_ =>
_.option("configFile", {
alias: "c",
type: "string",
default: defaultConfigFile,
}),
comm(
["get [key]", "g"],
"get config value",
Expand Down
7 changes: 5 additions & 2 deletions examples/complex/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ const configSubHandler = createHandlerFor(
cmd.$.config,
composeHandlers(
config.handler,
// extend config handler with self handler
createHandlerFor(
cmd.$.config.$.$self,
(): HandlerResult => async ({ yargs }) => {
console.log(`Config management`);
(argv): HandlerResult => async ({ yargs }) => {
console.log(`Config management: ${argv.configFile}`);
console.log();

yargs.showHelp();
},
),
Expand Down
5 changes: 4 additions & 1 deletion examples/complex/client/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { createMain } from "../../../src/main";
import { cmd, handler } from "./";

const main = createMain(cmd, handler.handle);
const main = createMain(
cmd,
({ args, yargs }) => handler.handle(args)({ yargs }),
);

if (require.main === module) {
main().catch(console.error);
Expand Down
8 changes: 5 additions & 3 deletions examples/complex/server/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import { HandlerResult } from "./types";

export const handler = createHandlerFor(
cmd,
(args): HandlerResult => async () => {
(args): HandlerResult => async ({ yargs }) => {
if (args.argv.debug) {
console.log("debug mode");
}

if (args.command === "start") {
if (args.command === undefined) {
yargs.showHelp();
}
else if (args.command === "start") {
const address = args.argv.address ?? { address: "0.0.0.0", port: 8080 };
console.log(
`start server at ${address.address}:${address.port}`,
Expand Down
5 changes: 4 additions & 1 deletion examples/complex/server/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import { createMain } from "../../../src/main";
import { cmd } from "./args";
import { handler } from "./handler";

const main = createMain(cmd, handler.handle);
const main = createMain(
cmd,
({ args, yargs }) => handler.handle(args)({ yargs }),
);

if (require.main === module) {
main().catch(console.error);
Expand Down
2 changes: 1 addition & 1 deletion examples/readme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const cmd = composeCommands(
serverStart,
serverStop,
subcommands(["config", "c"], "config management", config),
// allow command without a subcommand
// allow handling command without a subcommand
).selfHandle(true);

const { result, yargs } = buildAndParse(cmd, process.argv.slice(2));
Expand Down
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,3 @@ export {
} from "./parser/parser";

export const setDebugLevel = logging.setLevel;
// logging.setLevel("DEBUG");
24 changes: 17 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import * as E from "./common/either";
import { ErrorType } from "./common/error";
import { isObjectWithOwnProperty, isPromiseLike } from "./common/util";
import { HandlerFunction } from "./handler";
import { buildAndParse } from "./parser/parser";
import {
buildAndParse,
BuildAndParseResult,
YargsObject,
} from "./parser/parser";

/**
* @description show help and error message and exit with code 1
Expand Down Expand Up @@ -34,18 +38,24 @@ export function failClient(

export const createMain = <
TCommand extends Command,
THandler extends HandlerFunction<GetCommandArgs<TCommand>>,
>(
cmd: TCommand,
handler: THandler,
mainFunction: (result: {
yargs: YargsObject;
args: GetCommandArgs<TCommand>;
}) => Promise<void> | void,
) =>
async () => {
const { yargs, result } = buildAndParse(cmd, process.argv.slice(2));
const parseResult = buildAndParse(cmd, process.argv.slice(2));

if (E.isLeft(result)) {
failClient(yargs, result);
if (E.isLeft(parseResult.result)) {
failClient(parseResult.yargs, parseResult.result);
}
const res = handler(result.right);

const res = mainFunction({
yargs: parseResult.yargs,
args: parseResult.result.right,
});

if (isPromiseLike(res)) {
await res;
Expand Down
2 changes: 1 addition & 1 deletion src/parser/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export type YargsObject = y.Argv<EmptyRecord>;

const logger = log.getLogger("parser");

type BuildAndParseResult<TCommand extends Command> = {
export type BuildAndParseResult<TCommand extends Command> = {
result: E.Either<ErrorType, GetCommandArgs<TCommand>>;
yargs: y.Argv;
};
Expand Down

0 comments on commit 86b64c8

Please sign in to comment.