-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
229 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
{ | ||
"deno.enable": true, | ||
"editor.formatOnSave": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,43 +7,42 @@ | |
|
||
_Denomander_ is a solution for [Deno](https://deno.land) command-line interfaces. It is inspired from [commander.js](https://github.com/tj/commander.js) by [tj](https://github.com/tj) which is the node's version. | ||
|
||
> [__Lizard__ 🦎](https://github.com/siokas/denomander/wiki/Lizard-%F0%9F%A6%8E): There is a new, much simpler and much cleaner, way to define you cli commands and options. It is called [__Lizard__ 🦎](https://github.com/siokas/denomander/wiki/Lizard-%F0%9F%A6%8E) and it is inspired by Laravel's Artisan commands. [For more, follow the instructions...](https://github.com/siokas/denomander/wiki/Lizard-%F0%9F%A6%8E) . | ||
> Denomander is a [Deno](https://deno.land) project so it needs to have deno installed in your system. | ||
> Denomander is a [Deno](https://deno.land) project so it needs to have deno installed in your system. | ||
> If you don't there is a Dockerfile in the root of the project to create an image running deno | ||
> To use it just build the Docker file `docker build -t deno .` | ||
> Now you can run all the deno commands `docker run --rm -v $PWD:/app/ deno test` | ||
## Installation | ||
|
||
Using Nest Land | ||
|
||
```javascript | ||
import Denomander from "https://x.nest.land/[email protected]/mod.ts"; | ||
``` | ||
|
||
Using Deno Land | ||
|
||
```javascript | ||
import Denomander from "https://deno.land/x/denomander/mod.ts"; | ||
``` | ||
|
||
## Usage example | ||
|
||
At first initialize the app and optionally you may pass the name, description and version of the app. If not you can change them afterwards by setting the __app_name__, __app_description__ and __app_version__ variables. | ||
At first initialize the app and optionally you may pass the name, description and version of the app. If not you can change them afterwards by setting the **app_name**, **app_description** and **app_version** variables. | ||
|
||
```javascript | ||
const program = new Denomander( | ||
{ | ||
app_name: "My MY App", | ||
app_description: "My MY Description", | ||
app_version: "1.0.1" | ||
} | ||
); | ||
const program = new Denomander({ | ||
app_name: "My MY App", | ||
app_description: "My MY Description", | ||
app_version: "1.0.1", | ||
}); | ||
``` | ||
|
||
There are three option types: __commands__, __options__ and __required options__. | ||
There are three option types: **commands**, **options** and **required options**. | ||
|
||
### Options | ||
To set an option just call the __option()__ method passing __a) the sort and the long flag__ seperated by space and __b) the description__. The value can be accessed as properties. | ||
|
||
To set an option just call the **option()** method passing **a) the sort and the long flag** seperated by space and **b) the description**. The value can be accessed as properties. | ||
|
||
```javascript | ||
program | ||
|
@@ -52,13 +51,13 @@ program | |
.option("-p --port", "Define the port") | ||
.parse(Deno.args); | ||
|
||
if(program.address){ | ||
const port = program.port || "8000"; | ||
console.log(`Server is running on ${program.address}:${port}`); | ||
} | ||
if (program.address) { | ||
const port = program.port || "8000"; | ||
console.log(`Server is running on ${program.address}:${port}`); | ||
} | ||
``` | ||
|
||
__You may define the option's short and long flags by seperating them with either with a) space, b) comma, or c) | (vertical bar or "pipe")__ | ||
**You may define the option's short and long flags by seperating them with either with a) space, b) comma, or c) | (vertical bar or "pipe")** | ||
|
||
```javascript | ||
program | ||
|
@@ -71,7 +70,8 @@ console.log(`Server is running on ${program.address}:${program.port}`); | |
``` | ||
|
||
### Required Options | ||
The implementation of required option is exactly same as the optional option but you have to call the __requiredOption()__ method instead. | ||
|
||
The implementation of required option is exactly same as the optional option but you have to call the **requiredOption()** method instead. | ||
|
||
```javascript | ||
program | ||
|
@@ -80,11 +80,13 @@ program | |
.option("-a --address", "Define the address") | ||
.parse(Deno.args); | ||
|
||
// The port is required so it must have a value | ||
let address = program.address || "localhost"; | ||
console.log(`Server run on ${address}:${program.port}`); | ||
// The port is required so it must have a value | ||
let address = program.address || "localhost"; | ||
console.log(`Server run on ${address}:${program.port}`); | ||
``` | ||
|
||
### Global Options and Base Command Options | ||
|
||
You have the option to define options which belong to all commands (global option) and options which belong to no command (base command option ex. --help, --version) | ||
|
||
```javascript | ||
|
@@ -124,20 +126,21 @@ program | |
``` | ||
|
||
### Commands | ||
There are two ways to implement the commands. The first is to use an action handler by calling the __action()__ method immediately after the command definition passing the callback function and the second is with custom one-line implementation. __Multiple command arguments are now supported!__ | ||
|
||
There are two ways to implement the commands. The first is to use an action handler by calling the **action()** method immediately after the command definition passing the callback function and the second is with custom one-line implementation. **Multiple command arguments are now supported!** | ||
|
||
To define a command just call the .command() method and pass the command name (optionally you may also pass the description and a callback function but if not you may define them afterwards in their own methods). After the command you have the option to declare argument(s) inside brackets []. If you want a not required argument just append a question mark (?) after the name of the argument. | ||
|
||
```javascript | ||
program | ||
.command("mv [from] [to] [message?]", "Start the server") | ||
.action(({from, to, message}:any)=>{ | ||
// Do your actions here | ||
console.log(`File is moved from ${from} to ${to}`); | ||
if(message){ | ||
console.log("message") | ||
} | ||
}); | ||
.command("mv [from] [to] [message?]", "Start the server") | ||
.action(({ from, to, message }: any) => { | ||
// Do your actions here | ||
console.log(`File is moved from ${from} to ${to}`); | ||
if (message) { | ||
console.log("message"); | ||
} | ||
}); | ||
|
||
program.parse(Deno.args); | ||
|
||
|
@@ -152,18 +155,19 @@ program.parse(Deno.args); | |
program | ||
.command("clone [foldername]") | ||
.description("clone a repo") | ||
.action(({foldername}:any) => { | ||
.action(({ foldername }: any) => { | ||
console.log("The repo is cloned into: " + foldername); | ||
}); | ||
|
||
program.parse(Deno.args); | ||
``` | ||
|
||
#### Custom Implementation | ||
|
||
```javascript | ||
program.command("serve", "Start the server"); | ||
|
||
if(program.serve){ | ||
if (program.serve) { | ||
console.log("The server has started..."); | ||
} | ||
|
||
|
@@ -176,54 +180,51 @@ After the command declaration you have the option to declare as many aliases as | |
|
||
```javascript | ||
program | ||
.command("serve", "Start the server") | ||
.alias("server", "start-server") | ||
.action(()=>{ | ||
console.log("the server is started"); | ||
}); | ||
.command("serve", "Start the server") | ||
.alias("server", "start-server") | ||
.action(() => { | ||
console.log("the server is started"); | ||
}); | ||
|
||
program.parse(Deno.args); | ||
|
||
// Command action calback is called in all 3 command names (actual command and two aliases) | ||
``` | ||
|
||
|
||
### Option to change default commands (help, version) | ||
|
||
In order to change the default commands (help, version) just call the corresponding method. In case of help pass the command and the description but in case of version you may also pass the actual version of the app and after that the command and the description. | ||
In order to change the default commands (help, version) just call the corresponding method. In case of help pass the command and the description but in case of version you may also pass the actual version of the app and after that the command and the description. | ||
|
||
```javascript | ||
program.setVersion( | ||
"1.8.1", | ||
"-x --xversion", | ||
"Display the version of the app" | ||
); | ||
|
||
program.parse(args); | ||
program.setVersion("1.8.1", "-x --xversion", "Display the version of the app"); | ||
|
||
program.parse(args); | ||
``` | ||
|
||
## Customize error messages | ||
|
||
There are two ways to change the error messages. You may pass a fourth argument in new Denomander() constructor (errors object) or you may call the .errorMessages() method again passing the error messages in object. | ||
|
||
1. | ||
|
||
```javascript | ||
const program = new Denomander( | ||
{ | ||
app_name: "My MY App", | ||
app_description: "My MY Description", | ||
app_version: "1.0.1", | ||
errors: { | ||
INVALID_RULE: "Invalid Rule", | ||
OPTION_NOT_FOUND: "Option not found!", | ||
COMMAND_NOT_FOUND: "Command not found!", | ||
REQUIRED_OPTION_NOT_FOUND: "Required option is not specified!", | ||
REQUIRED_VALUE_NOT_FOUND: "Required command value is not specified!", | ||
TOO_MANY_PARAMS: "You have passed too many parameters", | ||
} | ||
const program = new Denomander({ | ||
app_name: "My MY App", | ||
app_description: "My MY Description", | ||
app_version: "1.0.1", | ||
errors: { | ||
INVALID_RULE: "Invalid Rule", | ||
OPTION_NOT_FOUND: "Option not found!", | ||
COMMAND_NOT_FOUND: "Command not found!", | ||
REQUIRED_OPTION_NOT_FOUND: "Required option is not specified!", | ||
REQUIRED_VALUE_NOT_FOUND: "Required command value is not specified!", | ||
TOO_MANY_PARAMS: "You have passed too many parameters", | ||
}, | ||
); | ||
}); | ||
``` | ||
|
||
2. | ||
|
||
```javascript | ||
program.errorMessages({ | ||
INVALID_RULE: "Invalid Rule", | ||
|
@@ -235,32 +236,45 @@ program.errorMessages({ | |
}); | ||
``` | ||
|
||
### Improved error experience. Option to throw the errors | ||
|
||
From v0.8 by default Denomander app does not throw the errors but instead it outputs the error message in the console and exits the app. If you want to throw all the errors just pass the `throw_errors: true` option inside the AppDetails in Denomander constructor | ||
|
||
```javascript | ||
const program = new Denomander({ | ||
app_name: "My App Name", | ||
app_description: "My App Description", | ||
app_version: "1.0.1", | ||
throw_errors: true, | ||
}); | ||
``` | ||
|
||
## ToDo | ||
|
||
- [X] Custom option processing | ||
- [ ] More examples | ||
- [ ] More tests | ||
- [X] Easy Error Customization | ||
- [ ] Documentation | ||
- [ ] Chanage --help default output | ||
- [X] Command with multiple arguments | ||
- [x] Custom option processing | ||
- [ ] More examples | ||
- [ ] More tests | ||
- [x] Easy Error Customization | ||
- [ ] Documentation | ||
- [ ] Chanage --help default output | ||
- [x] Command with multiple arguments | ||
|
||
## Used | ||
|
||
- [Deno](https://deno.land) | ||
- [Deno STD Libraries](https://deno.land/std/) | ||
- [FlatIcon](https://www.flaticon.com/) for the logo | ||
- [Deno](https://deno.land) | ||
- [Deno STD Libraries](https://deno.land/std/) | ||
- [FlatIcon](https://www.flaticon.com/) for the logo | ||
|
||
## Meta | ||
|
||
Apostolos Siokas – [@siokas_](https://twitter.com/siokas_) – [email protected] | ||
Apostolos Siokas – [@siokas\_](https://twitter.com/siokas_) – [email protected] | ||
|
||
## Contributing | ||
|
||
Any kind of contribution is welcome! | ||
|
||
## License | ||
|
||
Distributed under the [MIT License](https://github.com/siokas/denomander/blob/master/LICENSE). | ||
Distributed under the [MIT License](https://github.com/siokas/denomander/blob/master/LICENSE). | ||
|
||
[https://github.com/siokas/denomander](https://github.com/siokas/denomander) | ||
[https://github.com/siokas/denomander](https://github.com/siokas/denomander) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
export const test = Deno.test; | ||
export { parse } from "https://deno.land/std@0.81.0/flags/mod.ts"; | ||
export { parse } from "https://deno.land/std@0.83.0/flags/mod.ts"; | ||
export { | ||
blue, | ||
bold, | ||
green, | ||
red, | ||
yellow, | ||
} from "https://deno.land/std@0.81.0/fmt/colors.ts"; | ||
} from "https://deno.land/std@0.83.0/fmt/colors.ts"; | ||
export { | ||
assert, | ||
assertEquals, | ||
assertThrows, | ||
assertThrowsAsync, | ||
} from "https://deno.land/std@0.81.0/testing/asserts.ts"; | ||
} from "https://deno.land/std@0.83.0/testing/asserts.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.