Compare commits

..

1 commit

2 changed files with 19 additions and 4 deletions

View file

@ -2,12 +2,16 @@ import Telme from './telme';
import { ArgParser, ERunMode, ISimpleMessageOptions, ITaskOptions, IInitProfileOptions } from './utils'; import { ArgParser, ERunMode, ISimpleMessageOptions, ITaskOptions, IInitProfileOptions } from './utils';
import Config, { IProfileConfig } from './config/config'; import Config, { IProfileConfig } from './config/config';
import Init from './flows/init' import Init from './flows/init'
const { version } = require('../package.json');
async function main() { async function main() {
const parsed = ArgParser.parse(process.argv); const parsed = ArgParser.parse(process.argv);
let config: IProfileConfig; let config: IProfileConfig;
let options: any; let options: any;
switch (parsed.mode) { switch (parsed.mode) {
case ERunMode.VERSION:
console.log(`[${Config.APP_NAME}] version ${version}`);
break;
case ERunMode.HELP: case ERunMode.HELP:
printHelp(); printHelp();
break; break;
@ -34,11 +38,12 @@ async function main() {
function printHelp() { function printHelp() {
const cliFlags = ArgParser.CLI_OPTIONS; const cliFlags = ArgParser.CLI_OPTIONS;
const message = const message =
`${Config.APP_NAME} - A CLI Telegram message tool `${Config.APP_NAME} v${version} - A CLI Telegram message tool
[Usage]: $ ${Config.APP_NAME} <telme_options> <?command> <arguments> [Usage]: $ ${Config.APP_NAME} <telme_options> <?command> <arguments>
Options: Options:
\t ${cliFlags.versionFlags.join(', ')} \t\t Print ${Config.APP_NAME} version.
\t ${cliFlags.helpFlags.join(', ')} \t\t This help page. \t ${cliFlags.helpFlags.join(', ')} \t\t This help page.
\t ${cliFlags.profileFlags.join(', ')} \t\t Specify a profile to use. This is optional. defaults to 'default' profile. \t ${cliFlags.profileFlags.join(', ')} \t\t Specify a profile to use. This is optional. defaults to 'default' profile.
\t ${cliFlags.initFlags.join(', ')} \t\t Will generate a config file for a given profile (defaults to 'default' profile). \t ${cliFlags.initFlags.join(', ')} \t\t Will generate a config file for a given profile (defaults to 'default' profile).

View file

@ -1,6 +1,7 @@
import InvalidArgumentsError from "../errors/invalid_arguments.error"; import InvalidArgumentsError from "../errors/invalid_arguments.error";
const CLI_OPTIONS = { const CLI_OPTIONS = {
versionFlags: ['--version', '-v'],
helpFlags: ['--help', '-h'], helpFlags: ['--help', '-h'],
initFlags: ['--init', '-i'], initFlags: ['--init', '-i'],
profileFlags: ['--profile', '-p'], profileFlags: ['--profile', '-p'],
@ -13,7 +14,11 @@ export class ArgParser {
static parse(testArgs: string[] = null): IRunOptions { static parse(testArgs: string[] = null): IRunOptions {
let cliArgs = testArgs ? testArgs.slice(2) : process.argv.slice(2); let cliArgs = testArgs ? testArgs.slice(2) : process.argv.slice(2);
let tokens = tokenize(cliArgs); let tokens = tokenize(cliArgs);
if (tokens.help) { if (tokens.version) {
return {
mode: ERunMode.VERSION
}
} else if (tokens.help) {
return { return {
mode: ERunMode.HELP mode: ERunMode.HELP
} }
@ -49,13 +54,16 @@ export class ArgParser {
function tokenize(args: string[]) { function tokenize(args: string[]) {
let cliArgs = args; let cliArgs = args;
if (!cliArgs.length) throw new InvalidArgumentsError('Missing a command to run'); if (!cliArgs.length) throw new InvalidArgumentsError('Missing a command to run');
const tokens: ITokenizeArgs = { init: false, help: false }; const tokens: ITokenizeArgs = { init: false, help: false, version: false };
// ['node', 'execCommand', ...]; // ['node', 'execCommand', ...];
let mode: ERunMode = ERunMode.TASK_MESSAGE; let mode: ERunMode = ERunMode.TASK_MESSAGE;
while (cliArgs.length > 0) { while (cliArgs.length > 0) {
let tokenCursor = 1; let tokenCursor = 1;
const token = cliArgs[0]; const token = cliArgs[0];
if (CLI_OPTIONS.helpFlags.indexOf(token) >= 0) { if (CLI_OPTIONS.versionFlags.indexOf(token) >= 0) {
tokens.version = true;
return tokens;
} else if (CLI_OPTIONS.helpFlags.indexOf(token) >= 0) {
tokens.help = true; tokens.help = true;
return tokens; return tokens;
} else if (CLI_OPTIONS.initFlags.indexOf(token) >= 0) { } else if (CLI_OPTIONS.initFlags.indexOf(token) >= 0) {
@ -89,6 +97,7 @@ function tokenize(args: string[]) {
} }
interface ITokenizeArgs { interface ITokenizeArgs {
version: boolean;
help: boolean; help: boolean;
profile?: string; profile?: string;
init: boolean; init: boolean;
@ -122,6 +131,7 @@ export interface ITaskOptions extends IBasicOptions {
export enum ERunMode { export enum ERunMode {
VERSION,
HELP, HELP,
INIT, INIT,
SIMPLE_MESSAGE, SIMPLE_MESSAGE,