├── .learn └── README.md /.learn: -------------------------------------------------------------------------------- 1 | tags: 2 | - tag1 3 | languages: 4 | - language1 5 | resources: 0 -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CLI Interfaces 2 | 3 | ## The CLI Interface 4 | 5 | CLI Applications generally follow a similar interface or user experience pattern. Imagine a CLI version of Tic Tac Toe. From a player's perspective, they would start the game by executing the `bin` for the game. 6 | 7 | ``` 8 | $ bin/tictactoe 9 | ``` 10 | 11 | The program will execute and generally greet the user with some text output: 12 | 13 | ``` 14 | $ bin/tictactoe 15 | 16 | Hi! Welcome to Command Line Tic Tac Toe! Would you like to play? (Y/n) 17 | 18 | ``` 19 | 20 | The CLI will prompt the user for input and will hang until the user types something and presses enter. The CLI generally gives instructions for the expected input at a given prompt. In the example above, the greeting ends by asking the user if they would like to play. 21 | 22 | ### User Input 23 | 24 | The `(Y/n)` at the end is a common convention for telling the user that the following prompt is looking for a "Yes" or "No" input as an answer. It is also saying it expects that input as a single character, either `y` or `n`. The capital `Y` is suggesting the default input if the user types nothing and simply presses enter. Generally CLIs will accept "Yes"/"No" in many forms (`yes`, `no`, `N`, `n`) and still use the `y/n` convention. 25 | 26 | ``` 27 | $ bin/tictactoe 28 | 29 | Hi! Welcome to Command Line Tic Tac Toe! Would you like to play? (Y/n) 30 | y ↵ 31 | Great! Starting a new game... 32 | 33 | | | 34 | ----------- 35 | | | 36 | ----------- 37 | | | 38 | 39 | Please select a square by entering 1-9, 1 for the top left and 9 for the bottom right: 40 | 41 | ``` 42 | *↵ is a Carriage Return symbol and simply means the "Enter" key was pressed. It is not literal. In the line above it is used to mean that the user entered in the `y` character at an input prompt and then pressed enter.* 43 | 44 | For more complex interactions, the CLI must inform the user about the custom input prompt, just like in the example above: `Please select a square by entering 1-9, 1 for the top left and 9 for the bottom right:`. We just tell the player to enter a number, 1 through 9, to represent the square. 45 | 46 | ``` 47 | $ bin/tictactoe 48 | 49 | Hi! Welcome to Command Line Tic Tac Toe! Would you like to play? (Y/n) 50 | y ↵ 51 | Great! Starting a new game... 52 | 53 | | | 54 | ----------- 55 | | | 56 | ----------- 57 | | | 58 | 59 | Please select a square by entering 1-9, 1 for the top left and 9 for the bottom right: 60 | 5↵ 61 | 62 | O | | 63 | ----------- 64 | | X | 65 | ----------- 66 | | | 67 | 68 | Please select a square by entering 1-9, 1 for the top left and 9 for the bottom right: 69 | 7↵ 70 | 71 | O | | 72 | ----------- 73 | | X | 74 | ----------- 75 | X | | 76 | ``` 77 | *Etc...* 78 | 79 | That's a pretty common interface pattern. 80 | 81 | ### Program Loop 82 | 83 | CLI programs have to continue running and accepting input from the user until they are explicitly exited through sending a termination signal to the program by pressing `CTRL+C` (on OS X, on Windows and other environments it might be `ALT+C` or `COMMAND+C`), or by telling the program to quit or exit through some sort of input. 84 | 85 | Imagine a Command Line Jukebox application to browse and play music through the Command Line (like Spotify or iTunes). It might look like this when run: 86 | 87 | ``` 88 | $ bin/jukebox 89 | 90 | Hi! Welcome to Command Line Music. 91 | 92 | What would you like to do? 93 | I accept: list, play, help, and quit. 94 | help↵ 95 | 96 | Main Menu Commands: 97 | help - Brings up this dialog. 98 | list - Will list all the songs in my collection. 99 | play - Will prompt for a song to play and play that song. 100 | quit - Will exit this program 101 | 102 | What would you like to do? 103 | I accept: list, play, help, and quit. 104 | list↵ 105 | 106 | My songs are: 107 | 1. Shake It Off, by Taylor Swift 108 | 2. In An Aeroplane Over the Sea, by Neautral Milk Hotel 109 | 3. Reality Check, by Binary Star 110 | 4. Hey boy, hey girl, by the Chemical Brothers 111 | 112 | What would you like to do? 113 | I accept: list, play, help, and quit. 114 | play↵ 115 | 116 | Please enter the song number you would like to play: 117 | 3↵ 118 | 119 | Reality Check, by Binary Star is currently open in your browser. 120 | 121 | What would you like to do? 122 | I accept: list, play, help, and quit. 123 | ``` 124 | 125 | Within the transcript of this program you can see the structure of the main application loop. 126 | 127 | After the initial greeting, the application begins its main loop, which consists of: 128 | 129 | 1. Prompting the user for input: `What would you like to do?` 130 | 2. Defining the input interface: `I accept: list, play, help, and quit.` 131 | 3. Accepting user input by yielding to a prompt and waiting patiently for the user to press enter. *If the user never enters anything, the program will wait at this state forever until the process is otherwise terminated.* 132 | 4. Taking the user input and executing the appropriate sub-routine or procedure that represents that feature. If a user enters 'help', the program should print the help instructions. 133 | 5. Once that feature terminates, the program is back at the start of the main loop. 134 | 135 | Any CLI application you build that necessitates a non-binary interface ("Do you want to play Tic Tac Toe?" vs "What game would you like to play?") will have a main loop interface as described above. 136 |

View CLI Interfaces on Learn.co and start learning to code for free.

137 | --------------------------------------------------------------------------------