44 - [ Registering Commands] ( #register )
55- [ Writing Output] ( #writing-output )
66
7- As you may have noticed already, Alpas comes with a bunch of console commands—such as ` make:controller ` , ` make:job ` ,
8- ` route:list ` etc.—to assist you performing some tasks from the command-line. You run an Alpas command by
9- prepending it with ` alpas ` . To see a list of all the Alpas commands as well as a short description for
10- each, you can use ` alpas help ` command.
7+ As you may have already noticed, Alpas comes with a bunch of console commands—` make:controller ` , ` make:job ` ,
8+ ` route:list ` etc. to name a few—to assist you in performing some tasks from a command-line. You run an
9+ Alpas command by prepending it with ` alpas ` .
10+
11+ To see a list of all the Alpas commands as well as a short description for each, you can use ` alpas list ` command.
1112
1213<a name =" custom-commands " ></a >
1314### [ Custom Commands] ( #custom-commands )
1415
1516If the core Alpas commands are not enough for you, it is easy to create your own. Alpas actually wraps
1617[ clikt] [ clikt ] command library in its own thin wrapper. Clikt is very powerful library that makes
17- writing command line interfaces simple and intuitive.
18+ writing command line interfaces simple and intuitive. It has pretty much everything you would
19+ ever need to create powerful command-line interfaces.
1820
1921<a name =" simple-commands " ></a >
2022#### [ Simple Commands] ( #simple-commands )
2123
22- When writing a simple custom command, all you have to do is extend ` dev.alpas.console.Command ` class and override
23- the ` run() ` method. The ` Command ` constructor receives a number of optional parameters allowing you to
24- configure your commands the way you want it. This includes help text, summary text etc.
24+ When writing a simple custom command, all you have to do is extend ` dev.alpas.console.Command ` class and
25+ override the ` run() ` method. The ` Command ` constructor receives a number of optional parameters allowing
26+ you to configure your commands the way you want it. This includes help text, summary text etc.
2527
26- The easiest way to create a command is by using ` make:command ` Alpas command, which will generate a new
27- command under ` console/commands ` folder.
28+ The easiest way to create a command is by using ` make:command ` Alpas command, which will
29+ generate a new command under ` console/commands ` folder.
2830
2931``` bash
3032
3133$ alpas make:command GreetCommand
3234
3335```
3436
35- <span class =" line-numbers " data-start =" 2 " >
37+ <span class =" line-numbers " data-start =" 2 " data-file = " console/commands/GreetCommand.kt " >
3638
3739``` kotlin
3840
39- // console/commands/GreetCommand.kt
4041class GreetCommand : Command (name = " greet" , help = " Say hello." ) {
4142 private val name by argument()
4243 override fun run () {
@@ -48,7 +49,7 @@ class GreetCommand : Command(name = "greet", help = "Say hello.") {
4849
4950</span >
5051
51- After [ registering the above command] ( #register ) , you can call it like so:
52+ After [ registering this new command] ( #register ) , you can call it like so:
5253
5354``` bash
5455
@@ -61,22 +62,25 @@ $ alpas greet you
6162<a name =" generator-commands " ></a >
6263#### [ Generator Commands] ( #generator-commands )
6364
64- Generator commands are the commands that generate some files when invoked. ` make:command ` is actually an example
65- of a generator command and so is ` make:controller ` . While you can use a simple command like ` GreetCommand `
66- above to write a generator command, you have to wired few things to get it right.
65+ Generator commands create some files and folders when invoked. ` make:command ` is actually an example of a
66+ generator command and so is ` make:controller ` .
67+
68+ While you can use a simple command like ` GreetCommand ` above to write a generator
69+ command, you have to wired few things to get it right.
70+
71+ Instead of extending ` dev.alpas.console.Command ` class, you can extend ` dev.alpas.console.GeneratorCommand `
72+ class to make your life much easier while writing generator commands. While it may not always satisfy
73+ all your needs, but most of the times it does and even it doesn't, it's a good place to start.
6774
68- Instead of extending ` dev.alpas.console.Command ` class, you can extend ` dev.alpas.console.GeneratorCommand ` class
69- to make your life much easier while writing such generator commands. While it may not always satisfy all your
70- needs for writing a generator command, but most of the times it does. You can pass ` --generator ` or ` -g `
71- to ` make:command ` command to create a generator type command for you. Then all you have to do is
72- override one abstract method—` populateOutputFile() ` .
75+ You can pass ` --generator ` or ` -g ` to ` make:command ` command to create a generator type command
76+ for you. Then all you have to do is override one abstract method—` populateOutputFile() ` .
7377
7478Let's see an example of how we can write a ` make:sandwich ` generator command that creates a ` NameOfSandwich.kt `
75- file under ` sandwiches ` folder, creating the folder if it already doesn't exist.
79+ file under ` sandwiches ` folder, creating this folder if it already doesn't exist.
7680
7781<div class =" ordered-list " >
7882
79- 1 . Create a command
83+ 1 . Create the command itself:
8084
8185``` bash
8286
@@ -86,12 +90,11 @@ $ alpas make:command MakeSandwich -g
8690
87912 . Modify ` console/commands/MakeSandwich.kt ` file to:
8892
89- <span class =" line-numbers " data-start =" 2 " >
93+ <span class =" line-numbers " data-start =" 2 " data-file = " console/commands/MakeSandwich.kt " >
9094
9195
9296``` kotlin
9397
94- // console/commands/MakeSandwich.kt
9598class MakeSandwich (srcPackage : String ) :
9699 GeneratorCommand (srcPackage, name = " make:sandwich" , help = " Make a sandwich." ) {
97100
@@ -103,6 +106,7 @@ class MakeSandwich(srcPackage: String) :
103106
104107 val dir = " sandwiches"
105108 val file = File (sourceOutputPath(dir, * parentDirs), " ${filename.toPascalCase()} .kt" )
109+
106110 return OutputFile ()
107111 .target(file)
108112 .packageName(makePackageName(dir, * parentDirs))
@@ -131,36 +135,39 @@ class MakeSandwich(srcPackage: String) :
131135
132136</span >
133137
134- Notice that we have a couple of placeholders in the code—` StubPackageName ` and ` StubClazzName ` both of which will
135- be replaced with proper texts automatically when we invoke this command.
138+ Notice that we have a couple of placeholders in the code—` StubPackageName ` and ` StubClazzName ` both
139+ of which will be replaced with proper texts automatically when we actually run the command.
136140
1371413 . Register this command in ` ConsoleKernel ` class:
138142
139- <span class =" line-numbers " data-start =" 10 " >
143+ <span class =" line-numbers " data-start =" 10 " data-file = " ConsoleKernel.kt " >
140144
141145
142146``` kotlin
143147
144- // ConsoleKernel.kt
145148// ...
149+
146150override fun commands (app : Application ): List <Command > {
147151 return listOf (MakeSandwich (app.srcPackage))
148152}
153+
149154// ...
150155
151156```
152157
153158</span >
154159
155- 4 . Now we are ready to make a sandwich.
160+ 4 . Now we are ready to make ourselves a sandwich or two:
156161
157162``` bash
158163
159164$ alpas make:sandwich club
160165
161166```
162167
163- This command will generate a ` sandwiches/Club.kt ` file. You can actually create multiple sandwiches in one go:
168+ This command will generate a ` sandwiches/Club.kt ` file.
169+
170+ You can actually create multiple sandwiches in one go:
164171
165172``` bash
166173
@@ -177,31 +184,34 @@ After you have created your own commands, you must register them with the app ot
177184to you. You can register a command in a number of ways—the easiest one is by overriding ` commands() ` method
178185in ` ConsoleKernel ` class and returning a list of all your commands.
179186
180- <span class =" line-numbers " data-start =" 10 " >
187+ <span class =" line-numbers " data-start =" 10 " data-file = " ConsoleKernel.kt " >
181188
182189``` kotlin
183190
184- // ConsoleKernel.kt
185191// ...
192+
186193override fun commands (app : Application ): List <Command > {
187194 return listOf (GreetCommand ())
188195}
196+
189197// ...
190198
191199```
192200
193201</span >
194202
203+ An alternative way is by creating and registering a new [ Service Provider] ( /docs/service-providers ) .
204+
195205> /tip/ <span >Clikt comes with many other powerful features such as ` parameters ` , ` flags ` , ` choice options ` ,
196- ` input prompts ` , ` password masking ` and much more. The best part of it is that the features are properly
197- documented with lots of real-world examples. We highly recommend [ consulting its documentation] [ clikt ]
198- when creating your own commands.</span >
206+ > ` input prompts ` , ` password masking ` and much more. The best part of it is that the features are properly
207+ > documented with lots of real-world examples. We highly recommend [ consulting its documentation] [ clikt ]
208+ > when creating your own commands.</span >
199209
200210<a name =" writing-output " ></a >
201211### [ Writing Output] ( #writing-output )
202212
203- When you need to write some output to the console, you can use ` info ` , ` success ` , ` warning ` , and ` error ` methods.
204- Most of these methods not only respect ` --quiet ` flag but also use proper ANSI colors for their purpose.
213+ When you need to write some output to the console, you can use ` info ` , ` success ` , ` warning ` , and ` error `
214+ methods. Most of these methods respect ` --quiet ` flag and also use proper ANSI colors for their purpose.
205215
206216``` kotlin
207217
@@ -218,9 +228,10 @@ success("Yay! This is working.")
218228```
219229
220230If you need more control over coloring the output, you can use ` withColors() ` method. Alpas uses the full-featured
221- text styling [ Mordant] ( https://github.com/ajalt/ mordant) library for coloring. This allows you to color the
222- output anyway you want. Keep in mind that ` withColors() ` ** won't** print anything if ` --quiet ` flag is set.
231+ text styling [ Mordant] [ mordant ] library for coloring console outputs . This allows you to color the output
232+ anyway you want. Keep in mind that ` withColors() ` ** won't** print anything if ` --quiet ` flag is set.
223233
224- > /power/ <span >Alpas Console is proudly powered by [ Clikt] [ clikt ] .
234+ > /power/ <span >Alpas Console is proudly powered by [ Clikt] [ clikt ] and [ Mordant ] [ mordant ] .
225235
226236[ clikt ] : https://ajalt.github.io/clikt/
237+ [ mordant ] : https://github.com/ajalt/mordant
0 commit comments