| OLD | NEW |
| 1 --- | 1 --- |
| 2 layout: tutorial | 2 layout: tutorial |
| 3 title: "Write Command-Line Apps" | 3 title: "Write Command-Line Apps" |
| 4 description: "Basics for command-line apps" | 4 description: "Basics for command-line apps" |
| 5 has-permalinks: true | 5 has-permalinks: true |
| 6 tutorial: | 6 tutorial: |
| 7 id: dart-io | 7 id: dart-io |
| 8 next: httpserver | 8 next: httpserver |
| 9 next-title: "Write HTTP Clients & Servers" | 9 next-title: "Write HTTP Clients & Servers" |
| 10 prev: indexeddb/ | 10 prev: indexeddb/ |
| 11 prev-title: "Use IndexedDB" | 11 prev-title: "Use IndexedDB" |
| 12 --- | 12 --- |
| 13 | 13 |
| 14 {% capture whats_the_point %} | 14 {% capture whats_the_point %} |
| 15 | 15 |
| 16 * Command-line applications need to do input and output. | 16 * Command-line applications need to do input and output. |
| 17 * The dart:io library provides I/O functionality. | 17 * The dart:io library provides I/O functionality. |
| 18 * The args package helps define and parse command-line arguments. | 18 * The args package helps define and parse command-line arguments. |
| 19 * Most input and output requires the use of Streams. | 19 * Most input and output requires the use of Streams. |
| 20 * Streams provide a series of asynchronous data events. | 20 * Streams provide a series of asynchronous data events. |
| 21 * To handle asynchronous data, you need to use Futures. | 21 * To handle asynchronous data, you need to use Futures. |
| 22 | 22 |
| 23 {% endcapture %} | 23 {% endcapture %} |
| 24 | 24 |
| 25 {% capture sample_links %} | 25 {% capture sample_links %} |
| 26 | 26 |
| 27 <p> This tutorial features these examples:</p> | 27 This tutorial features the following example |
| 28 * helloworld | 28 under the **cmdline/bin** directory: |
| 29 * dcat | 29 |
| 30 * dcat.dart |
| 30 | 31 |
| 31 <p> | 32 <p> |
| 32 Don't have the source code? | 33 Don't have the source code? |
| 33 <a href="https://github.com/dart-lang/dart-tutorials-samples/archive/master.zip"
> | 34 <a href="https://github.com/dart-lang/dart-tutorials-samples/archive/master.zip"
> |
| 34 Download it. | 35 Download it. |
| 35 </a> | 36 </a> |
| 36 | 37 |
| 37 {% endcapture %} | 38 {% endcapture %} |
| 38 | 39 |
| 39 {% capture content %} | 40 {% capture content %} |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 | <a href="https://api.dartlang.org/args/ArgParser" target="_blank">ArgParser</a
> | A class that parses command-line arguments | | 198 | <a href="https://api.dartlang.org/args/ArgParser" target="_blank">ArgParser</a
> | A class that parses command-line arguments | |
| 198 | <a href="https://api.dartlang.org/args/ArgResults" target="_blank">ArgResults<
/a> | The result of parsing command-line arguments using ArgParser. | | 199 | <a href="https://api.dartlang.org/args/ArgResults" target="_blank">ArgResults<
/a> | The result of parsing command-line arguments using ArgParser. | |
| 199 {: .table } | 200 {: .table } |
| 200 | 201 |
| 201 Let's take a look at the `dcat` sample, | 202 Let's take a look at the `dcat` sample, |
| 202 which uses ArgParser and ArgResults to parse and store its command-line argument
s. | 203 which uses ArgParser and ArgResults to parse and store its command-line argument
s. |
| 203 | 204 |
| 204 <ol> | 205 <ol> |
| 205 <li markdown="1"> | 206 <li markdown="1"> |
| 206 Copy the sample file from the github repo: | 207 Copy the sample file from the github repo: |
| 207 <a href="https://github.com/dart-lang/dart-tutorials-samples/blob/master/bin/cmd
line/dcat.dart">dcat.dart</a>. | 208 <a href="https://github.com/dart-lang/dart-tutorials-samples/blob/master/cmdline
/bin/dcat.dart">dcat.dart</a>. |
| 208 </li> | 209 </li> |
| 209 | 210 |
| 210 <li markdown="1"> | 211 <li markdown="1"> |
| 211 Run the program from the command line as shown by the highlighted text. | 212 Run the program from the command line as shown by the **boldface** text. |
| 212 | 213 |
| 213 {% prettify bash %} | 214 <pre> |
| 214 $ [[highlight]]dart dcat.dart -n quotes.txt[[/highlight]] | 215 $ <b>dart dcat.dart -n quotes.txt</b> |
| 215 1 Be yourself. Everyone else is taken. -Oscar Wilde | 216 1 Be yourself. Everyone else is taken. -Oscar Wilde |
| 216 2 Don't cry because it's over, smile because it happened. -Dr. Seuss | 217 2 Don't cry because it's over, smile because it happened. -Dr. Seuss |
| 217 3 You only live once, but if you do it right, once is enough. -Mae West | 218 3 You only live once, but if you do it right, once is enough. -Mae West |
| 218 ... | 219 ... |
| 219 {% endprettify %} | 220 </pre> |
| 220 | 221 |
| 221 The program displays the contents of the source code file and | 222 The program displays the contents of the source code file and |
| 222 preceeds each line with a line number. | 223 preceeds each line with a line number. |
| 223 </li> | 224 </li> |
| 224 | 225 |
| 225 </ol> | 226 </ol> |
| 226 | 227 |
| 227 The following diagram shows how the `dcat` command line used above | 228 The following diagram shows how the `dcat` command line used above |
| 228 is parsed into the `ArgResults` object. | 229 is parsed into the `ArgResults` object. |
| 229 | 230 |
| 230  | 231  |
| 231 | 232 |
| 232 You can access flags and options by name, | 233 You can access flags and options by name, |
| 233 treating the ArgResults object like a Map. | 234 treating the ArgResults object like a Map. |
| 234 You can access other values with properties such as `rest`. | 235 You can access other values with properties such as `rest`. |
| 235 | 236 |
| 236 Here's the code from `dcat` that deals with command-line arguments: | 237 Here's the code from `dcat` that deals with command-line arguments: |
| 237 | 238 |
| 238 <pre class="prettyprint lang-dart"> | 239 <pre class="prettyprint lang-dart"> |
| 239 | |
| 240 ... | 240 ... |
| 241 <a href="#" class="dart-popover" data-toggle="popover" title="Parsed arguments"
data-html="true" data-trigger="hover focus" data-content="This object contains p
arsed options and flags.">ArgResults argResults;</a> | 241 <a href="#" class="dart-popover" data-toggle="popover" title="Parsed arguments"
data-html="true" data-trigger="hover focus" data-content="This object contains p
arsed options and flags.">ArgResults argResults;</a> |
| 242 | 242 |
| 243 void main(<a href="#" class="dart-popover" data-toggle="popover" title="Command-
line arguments" data-html="true" data-trigger="hover focus" data-content="The sy
stem passes command-line arguments into the program in a list of strings.">List&
lt;String> arguments</a>) { | 243 void main(<a href="#" class="dart-popover" data-toggle="popover" title="Command-
line arguments" data-html="true" data-trigger="hover focus" data-content="The sy
stem passes command-line arguments into the program in a list of strings.">List&
lt;String> arguments</a>) { |
| 244 final parser = new ArgParser() | 244 final parser = new ArgParser() |
| 245 <a href="#" class="dart-popover" data-toggle="popover" title="Define a val
id flag" data-html="true" data-trigger="hover focus" data-content="Add a flag de
finition to the command-line argument parser. This code defines the flag -n, whi
ch when used displays line numbers.">..addFlag(LINE_NUMBER, negatable: false, ab
br: 'n')</a>; | 245 <a href="#" class="dart-popover" data-toggle="popover" title="Define a val
id flag" data-html="true" data-trigger="hover focus" data-content="Add a flag de
finition to the command-line argument parser. This code defines the flag -n, whi
ch when used displays line numbers.">..addFlag(LINE_NUMBER, negatable: false, ab
br: 'n')</a>; |
| 246 | 246 |
| 247 argResults = parser.<a href="#" class="dart-popover" data-toggle="popover" tit
le="Parse the arguments" data-html="true" data-trigger="hover focus" data-conten
t="Parse the arguments that were passed into the main() function. The parser sto
ps parsing if it finds an undefined option or flag.">parse(arguments)</a>; | 247 argResults = parser.<a href="#" class="dart-popover" data-toggle="popover" tit
le="Parse the arguments" data-html="true" data-trigger="hover focus" data-conten
t="Parse the arguments that were passed into the main() function. The parser sto
ps parsing if it finds an undefined option or flag.">parse(arguments)</a>; |
| 248 List<String> paths = <a href="#" class="dart-popover" data-toggle="popov
er" title="Remaining arguments" data-html="true" data-trigger="hover focus" data
-content="To get the arguments that remain after parsing all of the valid option
s and flags, use the rest property.">argResults.rest</a>; | 248 List<String> paths = <a href="#" class="dart-popover" data-toggle="popov
er" title="Remaining arguments" data-html="true" data-trigger="hover focus" data
-content="To get the arguments that remain after parsing all of the valid option
s and flags, use the rest property.">argResults.rest</a>; |
| 249 | 249 |
| (...skipping 385 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 635 [Write HTTP Clients & Servers](/docs/tutorials/httpserver/). | 635 [Write HTTP Clients & Servers](/docs/tutorials/httpserver/). |
| 636 | 636 |
| 637 * The [Get Input from a Form](/docs/tutorials/forms/) tutorial | 637 * The [Get Input from a Form](/docs/tutorials/forms/) tutorial |
| 638 features a client-server. | 638 features a client-server. |
| 639 The code for the server, which uses CORS headers and handles | 639 The code for the server, which uses CORS headers and handles |
| 640 POST requests, is explained in detail. | 640 POST requests, is explained in detail. |
| 641 | 641 |
| 642 {% endcapture %} | 642 {% endcapture %} |
| 643 | 643 |
| 644 {% include tutorial.html %} | 644 {% include tutorial.html %} |
| OLD | NEW |