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 Config, { IProfileConfig } from './config/config';
import Init from './flows/init'
const { version } = require('../package.json');
async function main() {
const parsed = ArgParser.parse(process.argv);
let config: IProfileConfig;
let options: any;
switch (parsed.mode) {
case ERunMode.VERSION:
console.log(`[${Config.APP_NAME}] version ${version}`);
break;
case ERunMode.HELP:
printHelp();
break;
@ -34,11 +38,12 @@ async function main() {
function printHelp() {
const cliFlags = ArgParser.CLI_OPTIONS;
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>
Options:
\t ${cliFlags.versionFlags.join(', ')} \t\t Print ${Config.APP_NAME} version.
\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.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";
const CLI_OPTIONS = {
versionFlags: ['--version', '-v'],
helpFlags: ['--help', '-h'],
initFlags: ['--init', '-i'],
profileFlags: ['--profile', '-p'],
@ -13,7 +14,11 @@ export class ArgParser {
static parse(testArgs: string[] = null): IRunOptions {
let cliArgs = testArgs ? testArgs.slice(2) : process.argv.slice(2);
let tokens = tokenize(cliArgs);
if (tokens.help) {
if (tokens.version) {
return {
mode: ERunMode.VERSION
}
} else if (tokens.help) {
return {
mode: ERunMode.HELP
}
@ -49,13 +54,16 @@ export class ArgParser {
function tokenize(args: string[]) {
let cliArgs = args;
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', ...];
let mode: ERunMode = ERunMode.TASK_MESSAGE;
while (cliArgs.length > 0) {
let tokenCursor = 1;
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;
return tokens;
} else if (CLI_OPTIONS.initFlags.indexOf(token) >= 0) {
@ -89,6 +97,7 @@ function tokenize(args: string[]) {
}
interface ITokenizeArgs {
version: boolean;
help: boolean;
profile?: string;
init: boolean;
@ -122,6 +131,7 @@ export interface ITaskOptions extends IBasicOptions {
export enum ERunMode {
VERSION,
HELP,
INIT,
SIMPLE_MESSAGE,