Skip to content

Latest commit

 

History

History
187 lines (111 loc) · 3.48 KB

File metadata and controls

187 lines (111 loc) · 3.48 KB

CLI Overview

The trie system comes with a command-line interface that can:

  1. Add a keyword to the trie

  2. Delete a keyword from the trie

  3. Search for a keyword in trie

  4. Generate completions for a prefix

  5. Display the trie

  6. Clear the trie

Installation

Prerequisites:

  • If you don't have Go installed, please follow the instructions on golang.org/doc/install before proceeding.

  • If you have a command called trie installed, please uninstall it before proceeding.

To install the CLI, run the following:

go get -u github.com/thomasbreydo/trieapi/cli/trie

Usage

You can use the CLI to do any of the following:

  1. Add a keyword to the trie

  2. Delete a keyword from the trie

  3. Search for a keyword in trie

  4. Generate completions for a prefix

  5. Display the trie

  6. Clear the trie

At any time, run trie help or trie help <command> to show usage info.

Add

To add a keyword to the trie, use add:

trie add --word <keyword>

or

trie add -w <keyword>

If the keyword is already in the trie, the CLI outputs Keyword (<keyword>) present. Otherwise, it outputs Keyword (<keyword>) added.

Delete

To delete a keyword from the trie, use delete:

trie delete --word <keyword>

or

trie delete -w <keyword>

If the keyword isn't in the trie, the CLI outputs Keyword (<keyword>) missing. Otherwise, it outputs Keyword (<keyword>) deleted.

Search

To check if a keyword is present in the trie, use search:

trie search --word <keyword>

or

trie search -w <keyword>

If the keyword is in the trie, the CLI outputs Keyword (<keyword>) found. Otherwise, it outputs Keyword (<keyword>) not found.

Complete

To get all keywords in the trie that start with a prefix, use complete:

trie complete --prefix <prefix>

or

trie complete -p <prefix>

Output:

  • By default, the CLI outputs a newline-separated list keywords

  • To output the list in JSON format, use the --json flag.

⚠️ If the trie contains whitespace-only entries, you may want to use --json to avoid ambiguity.

Display

To display the trie, use display:

trie display

Output:

  • By default, the CLI outputs a newline-separated list of all words in the trie.

  • To output the list in JSON format, use the --json flag.

⚠️ If the trie contains whitespace-only entries, you may want to run --json to avoid ambiguity.

Clear

To delete all keywords from the trie, use clear:

trie clear

Output:

  • If the trie is already empty, the CLI outputs Already empty.

  • Otherwise, it outputs Trie cleared.

Edge cases

The empty string

To set --word to be the empty string, do the following:

trie <add/delete/search> --word ""

This also works for -w and --prefix.

If the empty string is in the trie, the first line the output of trie display will be blank.

Strings with spaces

When using the shell, spaces must be escaped. For example, if you want -w, --word or --prefix to be a b', you must do the following:

trie add -word "a b'"

Strings with non-ASCII characters

The CLI URL-encodes all keywords to ensure Unicode compatibility.

Auto-generated CLI Reference

See auto/trie.md for an auto-generated CLI reference.