-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.xml
344 lines (244 loc) · 16 KB
/
index.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>Swiftline</title>
<link>http://localhost:1313/</link>
<description>Recent content on Swiftline</description>
<generator>Hugo -- gohugo.io</generator>
<language>en-us</language>
<lastBuildDate>Mon, 30 Nov 2015 20:32:35 +0000</lastBuildDate>
<atom:link href="http://localhost:1313/index.xml" rel="self" type="application/rss+xml" />
<item>
<title>Whats included</title>
<link>http://localhost:1313/post/included/</link>
<pubDate>Mon, 30 Nov 2015 20:32:35 +0000</pubDate>
<guid>http://localhost:1313/post/included/</guid>
<description><p>Swiftline consists the following:</p>
<ul>
<li><strong>Colorize</strong>: Helps adding colors to strings written to the terminal</li>
<li><strong>Ask</strong>: Choose and agree: Easily create prompt for asking the user more info</li>
<li><strong>Run</strong>: A quick way to run an external command and read its standard output and standard error.</li>
<li><strong>Env</strong>: Read and write environment variables <a href="http://ruby-doc.org/core-2.2.0/ENV.html">ruby-flavored</a></li>
<li><strong>Args</strong>: Parses command line arguments and return a hash of the passed flags</li>
<li>More components to come&hellip;.</li>
</ul>
</description>
</item>
<item>
<title>Installation</title>
<link>http://localhost:1313/post/installation/</link>
<pubDate>Fri, 30 Oct 2015 20:32:35 +0000</pubDate>
<guid>http://localhost:1313/post/installation/</guid>
<description>
<p>You can install Swiftline using cocoapods,</p>
<h3 id="cocoapods:4c40a97dcf30b230e73eabba1323c21c">Cocoapods</h3>
<pre><code>use_frameworks!
pod 'Swiftline'
</code></pre>
<h3 id="carthage:4c40a97dcf30b230e73eabba1323c21c">Carthage</h3>
<pre><code>github 'Swiftline/Swiftline'
</code></pre>
<h3 id="cocoapods-rome-plugin:4c40a97dcf30b230e73eabba1323c21c">Cocoapods + Rome plugin</h3>
<p>If you want to use swiftline in a script you can use <a href="https://github.com/neonichu/Rome">Rome</a> cocoapod plugin. This plugin builds the framework from the pod file and place them in a Rome directory.</p>
<pre><code>platform :osx, '10.10'
plugin 'cocoapods-rome'
pod 'Swiftline'
</code></pre>
<h3 id="examples:4c40a97dcf30b230e73eabba1323c21c">Examples</h3>
<p>A list of examples can be found on <a href="https://github.com/oarrabi/Swiftline/tree/master/Examples">github</a></p>
</description>
</item>
<item>
<title>Usage</title>
<link>http://localhost:1313/other/usage/</link>
<pubDate>Fri, 30 Oct 2015 20:32:35 +0000</pubDate>
<guid>http://localhost:1313/other/usage/</guid>
<description>
<h2 id="colorize:dc32fe45042973266ca60588597e0b66">Colorize 🎨</h2>
<p>Colorize helps styling the strings before printing them to the terminal. You can change the text color, the text background and the text style.
Colorize works by extending <code>String</code> struct to add styling to it.</p>
<p>To change the text color, use either <code>string.f</code> or <code>string.foreground</code>:</p>
<pre><code class="language-swift">print(&quot;Red String&quot;.f.Red)
print(&quot;Blue String&quot;.foreground.Blue)
</code></pre>
<p>To change the text background color, use either <code>string.b</code> or <code>string.background</code>:</p>
<pre><code class="language-swift"> print(&quot;I have a white background&quot;.b.White)
print(&quot;My background color is green&quot;.background.Green)
</code></pre>
<p>To change the text background style, use either <code>string.s</code> or <code>string.style</code>:</p>
<pre><code class="language-swift"> print(&quot;I am a bold string&quot;.s.Bold)
print(&quot;I have an underline&quot;.style.Underline)
</code></pre>
<p>You can compose foreground, background, and style:</p>
<pre><code class="language-swift"> print(&quot;I am an underlined red on white string&quot;.s.Underline.f.Red.b.White)
</code></pre>
<h2 id="ask-choose-agree:dc32fe45042973266ca60588597e0b66">Ask, Choose, Agree ❓</h2>
<p>Ask, Choose and Agree are used to prompt the user for more information.</p>
<h3 id="ask:dc32fe45042973266ca60588597e0b66">Ask</h3>
<p>Ask presents the user with a prompt and waits for the user input.</p>
<pre><code class="language-swift"> let userName = ask(&quot;Enter user name?&quot;)
</code></pre>
<p><code>userName</code> will contain the name entered by the user</p>
<p>Ask can be used to ask for value of Int, Double or Float types, to ask for an integer for example:</p>
<pre><code class="language-swift"> let age = ask(&quot;How old are you?&quot;, type: Int.self)
</code></pre>
<p>If the user prints something thats not convertible to integer, a new prompt is displayed to him, this prompt will keep displaying until the user enters an Int:</p>
<pre><code>How old are you?
None
You must enter a valid Integer.
? Error
You must enter a valid Integer.
? 5
5
</code></pre>
<p>Validations are added by calling <code>addInvalidCase</code> on <code>AskSettings</code>.</p>
<pre><code class="language-swift"> let name = ask(&quot;Who are you?&quot;) { settings in
settings.addInvalidCase(&quot;Snuffles is not allowed&quot;) { value in
value.containsString(&quot;Snuffles&quot;)
}
}
</code></pre>
<p>If the user entered <code>Snuffles</code> ask will keep displaying the invalid message passed to <code>addInvalidCase</code></p>
<pre><code>Who are you?
Snuffles
Snuffles is not allowed
? Snuffles
Snuffles is not allowed
? Snowball
Your name is Snowball
</code></pre>
<p><code>AskSettings.confirm</code> will ask the user to confirm his choice after entering it</p>
<pre><code class="language-swift"> let name = ask(&quot;Who are you?&quot;) { settings in
settings.confirm = true
}
</code></pre>
<p>The above will output:</p>
<pre><code>Who are you?
Snuffles
Are you sure? YES
Your name is Snuffles
</code></pre>
<h3 id="choose:dc32fe45042973266ca60588597e0b66">Choose</h3>
<p>Choose is used to prompt the user to select an item between several possible items.</p>
<p>To display a choice of programming lanaugage for example:</p>
<pre><code class="language-swift"> let choice = choose(&quot;Whats your favorite programming language? &quot;,
choices: &quot;Swift&quot;, &quot;Objective C&quot;, &quot;Ruby&quot;, &quot;Python&quot;, &quot;Java :S&quot;)
</code></pre>
<p>This will print:</p>
<pre><code>1. Swift
2. Objective C
3. Ruby
4. Python
5. Java :S
Whats your favorite programming language?
</code></pre>
<p>The user can either choose the numbers (1..5) or the item itself. If the user enters a wrong input. A prompt will keep showing until the user makes a correct choice</p>
<pre><code>Whats your favorite programming language? JavaScript
You must choose one of [1, 2, 3, 4, 5, Swift, Objective C, Ruby, Python, Java :S].
? BBB
You must choose one of [1, 2, 3, 4, 5, Swift, Objective C, Ruby, Python, Java :S].
? Swift
You selected Swift, good choice!
</code></pre>
<p>You can customize the return value for each choice element. For example if you want to get an Int from the choice, you would do this</p>
<pre><code class="language-swift"> let choice = choose(&quot;Whats your favorite programming language? &quot;, type: Int.self) { settings in
settings.addChoice(&quot;Swift&quot;) { 42 }
settings.addChoice(&quot;Objective C&quot;) { 20 }
}
</code></pre>
<p>The number on the left can be changed to letters, here is how you could do that:</p>
<pre><code class="language-siwft"> let choice = choose(&quot;Whats your favorite programming language? &quot;, type: String.self) { settings in
//choice value will be set to GOOD
settings.addChoice(&quot;Swift&quot;) { &quot;GOOD&quot; }
//choice value will be set to BAD
settings.addChoice(&quot;Java&quot;) { &quot;BAD&quot; }
settings.index = .Letters
settings.indexSuffix = &quot; ----&gt; &quot;
}
</code></pre>
<p>That will print:</p>
<pre><code>a ----&gt; Swift
b ----&gt; Java
Whats your favorite programming language?
</code></pre>
<h3 id="agree:dc32fe45042973266ca60588597e0b66">Agree</h3>
<p>Agree is used to ask a user for a Yes/No question. It returns a boolean representing the user input.</p>
<pre><code class="language-swift"> let choice = agree(&quot;Are you sure you want to `rm -rf /` ?&quot;)
</code></pre>
<p>If the user enters any invalid input, agree will keep prompting him for a Yes/No question</p>
<pre><code>Are you sure you want to `rm -rf /` ? What!
Please enter &quot;yes&quot; or &quot;no&quot;.
Are you sure you want to `rm -rf /` ? Wait
Please enter &quot;yes&quot; or &quot;no&quot;.
Are you sure you want to `rm -rf /` ? No
You entered false
</code></pre>
<h2 id="run:dc32fe45042973266ca60588597e0b66">Run 🏃</h2>
<p>Run provides a quick, concise way to run an external command and read its standard output and standard error.</p>
<p>To execute a simple command you would do:</p>
<pre><code class="language-swift"> let result = run(&quot;ls -all&quot;)
print(result.stdout)
</code></pre>
<p><code>result</code> type is <code>RunResults</code>, it contains:</p>
<ul>
<li><code>exitStatus</code>: The command exit status</li>
<li><code>stdout</code>: The standard output for the command executed</li>
<li><code>stderr</code>: The standard error for the command executed</li>
</ul>
<p>While <code>run(&quot;command&quot;)</code> can split the arguments by spaces. Some times argument splitting is not trivial. If you have multiple argument to pass to the command to execute, you should use <code>run(command: String, args: String...)</code>. The above translates to:</p>
<pre><code class="language-swift"> let result = run(&quot;ls&quot;, args: &quot;-all&quot;)
</code></pre>
<p>To customize the run function, you can pass in a customization block:</p>
<pre><code class="language-swift"> let result = run(&quot;ls -all&quot;) { settings in
settings.dryRun = true
settings.echo = [.Stdout, .Stderr, .Command]
settings.interactive = false
}
</code></pre>
<p><code>settings</code> is an instance of RunSettings, which contains the following variables:</p>
<ul>
<li><code>settings.dryRun</code>: defaults to false. If false, the command is actually run. If true, the command is logged to the stdout paramter of result</li>
<li><code>settings.echo</code>: Customize the message printed to stdout, <code>echo</code> can contain any of the following:
<ul>
<li><code>EchoSettings.Stdout</code>: The stdout returned from running the command will be printed to the terminal</li>
<li><code>EchoSettings.Stderr</code>: The stderr returned from running the command will be printed to the terminal</li>
<li><code>EchoSettings.Command</code>: The command executed will be printed to the terminal</li>
</ul></li>
<li><code>settings.interactive</code>: defaults to false. If set to true the command will be executed using <code>system</code> kernel function and only the exit status will be captured. If set to false, the command will be executed using <code>NSTask</code> and both stdout and stderr will be captured.
Set <code>interactive</code> to true if you expect the launched command to ask input from the user through the stdin.</li>
</ul>
<p><code>runWithoutCapture(&quot;command&quot;)</code> is a quick way to run a command in interactive mode. The return value is the exit code of that command.</p>
<h2 id="env:dc32fe45042973266ca60588597e0b66">Env</h2>
<p>Env is used to read and write the environment variables passed to the script</p>
<pre><code class="language-swift">// Set enviroment variable
Env.set(&quot;key1&quot;, &quot;value1&quot;)
// Get environment variable
Env.get(&quot;SomeKey&quot;)
// Clear all variables
Env.clear()
// Get all keys and values
Env.keys()
Env.values()
</code></pre>
<h2 id="args:dc32fe45042973266ca60588597e0b66">Args</h2>
<p>Returns the arguments passed to the script. For example when calling <code>script -f1 val1 -f2 val2 -- val3 val4</code></p>
<p><code>Args.all</code> returns an array of all the raw arguments, in this example it will be <code>[&quot;-f1&quot;, &quot;val1&quot;, &quot;-f2&quot;, &quot;val2&quot;, &quot;--&quot;, &quot;val3&quot;, &quot;val4&quot;</code></p>
<p><code>Args.parsed</code> returns a structure that contains a parsed map of arguments and an array of arguments, for this example:</p>
<p><code>Args.parsed.parameters</code> returns <code>[&quot;val3&quot;, &quot;val4&quot;]</code></p>
<p><code>Args.parsed.flags</code> returns a dictinary of flags <code>[&quot;f1&quot;: &quot;val1&quot;, &quot;f2&quot;, &quot;val2&quot;]</code></p>
</description>
</item>
<item>
<title>Get help</title>
<link>http://localhost:1313/post/join/</link>
<pubDate>Mon, 30 Nov 2015 20:32:35 +0000</pubDate>
<guid>http://localhost:1313/post/join/</guid>
<description><ul>
<li>Join the chat on <a href="https://gitter.im/Swiftline?utm_source=share-link&amp;utm_medium=link&amp;utm_campaign=share-link">gitter</a></li>
<li>Ping the author on twitter <a href="http://twitter.com/@ifnottrue">@ifnottrue</a></li>
<li>Submit an <a href="https://github.com/Swiftline/Swiftline/issues/new">issue</a> on github</li>
</ul>
</description>
</item>
</channel>
</rss>