0.1.1 • Published 5 years ago

micro-command v0.1.1

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

Micro-Command is a minimal command-line interpreter

Micro-Command is not flashy but it will do the job swiftly and quickly. Unlike other interpreters feed in a string and recieve it as an object, great for live interactions and not initialisations. You do not need to initialise any commands, it will simply return the command as an object with any variables.

Example apps

save.js

const { MicroCLI } = require('micro-command');
let Micro = new MicroCLI();

let data = "Example data";
Micro.Parse("save --file C:\\Users\\x\\Documents --title \"Hello world\" -vo").then(x => {
    if (x.save) {
        if (x.save.file) {
            if (x.save.v || x.save.verbose) console.log(`Saving ${x.save.title ? x.save.title : "Placeholder"} to ${x.save.file}`);
            saveFunction(data, x.save.file, x.save.title ? x.save.title ? "Placeholder");
            if (x.save.v || x.save.verbose) console.log(`Saved ${x.save.title ? x.save.title : "Placeholder"} to ${x.save.file}`);
            if (x.save.o || x.save.open) {
                if (x.save.v || x.save.verbose) console.log(`Opening ${x.save.file}`);
                openFunction(x.save.file);
            }
        }
    } else if (x.delete) {
        //Do deletion stuff
    }
});

Parse Object (x)

{ 
	save: { 
		file: 'C:\\Users\\x\\Documents',
		title: 'Hello world',
		v: true,
		o: true 
	} 
}

account.js

const { MicroCLI } = require('micro-command');
let Micro = new MicroCLI();

Micro.Parse("account history --no-time").then(x => {
    console.log(x);
    if (x.account) {
        if (x.account.history) {
            console.log(getHistory(x.account.time)); //Don't output time
        }
    }
});

Parse Object (x)

{ 
	account: { 
		history: { 
			time: false
		}
	}
}

Command structure

There are 3 components to a command: command, flag, variable No part is required, parsing just commands, or just options and variables is fine.

Command

These are at the start of the command, any word not prefixed with - or " will be interpreted as a command, in the Parse return commands are objects.

In this example both foo and bar are commands

foo bar --baz qux

Flag

Flags are represented by a prefixed double hyphen, hyphen -- or --- is used for word options such as --file- is used for single char options such as -v or -vo Single char options can be grouped, so -vo will act as 2 variables v and o - both being true

To negate options append no- to the prefix, this will turn everything into false, i.e. --no-open, -no-o, -no-vo

Variable

Variables pair to the option before, --number 4 will resolve to { number: 4 }. Variables can be strings, numbers, or booleans. Variables cannot be paired with multiple single character options. -xyz foo is not allowed, -x foo is allowed.

String

Multi-word strings must be surrounded by speech " marks, single words do not

--title Example

--title "Hello World"

-t Foo

--file C:\\Users\\x\\Documents

####Numbers Integers and floats can be used and will be resolved into a number type

--size 4

--size 1.2

-s 5

####Booleans By default by including a flag sets it to true, prefixing the flag with no- will set the flag to false, not mentioning a flag leaves it as null; to check if a flag is false check x.bool != null && !x.bool

--verbose
--verbose true

-no-v
-v false