Added Short representations and Help Flag

This commit is contained in:
2025-05-22 23:25:53 -04:00
parent 2e770df8f3
commit 421090b673
3 changed files with 75 additions and 28 deletions

View File

@@ -9,6 +9,7 @@ A simple command-line Todo application written in Go. Todos are stored in a SQLi
- Mark todos as completed - Mark todos as completed
- Remove todos by ID - Remove todos by ID
- Data is persisted in SQLite database - Data is persisted in SQLite database
- User-friendly command-line interface with action commands
## Dependencies ## Dependencies
@@ -23,44 +24,52 @@ Build the app:
go build -o todo go build -o todo
``` ```
### Add a Todo ### Command Structure
The app uses a command-based interface where the action comes first, followed by flags:
```sh ```sh
./todo -action=add -title="Buy milk" -description="Get 2 liters of milk" ./todo [action] [flags]
``` ```
### List Todos Available actions:
- `add` - Add a new todo item
- `remove` - Remove a todo item
- `complete` - Mark a todo item as completed
- `list` - List all todo items
### Flags
- `-t, --title` - Title for the todo (required for add)
- `-d, --description` - Description for the todo (required for add)
- `-i, --id` - ID of the todo (required for remove and complete)
- `-h, --help` - Display help information
### Examples
Add a new todo:
```sh ```sh
./todo -action=list ./todo add -t "Buy milk" -d "Get 2 liters of milk"
``` ```
### Complete a Todo List all todos:
```sh ```sh
./todo -action=complete -id=1 ./todo list
``` ```
### Remove a Todo Complete a todo:
```sh ```sh
./todo -action=remove -id=1 ./todo complete -i 1
``` ```
## Flags Remove a todo:
- `-action` (add, remove, list, complete) — What you want to do
- `-title` — Title for the todo (required for add)
- `-description` — Description for the todo (required for add)
- `-id` — ID of the todo (required for remove and complete)
## Example
```sh ```sh
./todo -action=add -title="Read Go book" -description="Finish chapter 3" ./todo remove -i 2
./todo -action=list ```
./todo -action=complete -id=1
./todo -action=remove -id=1 Display help:
```sh
./todo -h
``` ```
## Next Improvements ## Next Improvements
@@ -70,3 +79,6 @@ go build -o todo
- Add unit tests - Add unit tests
- Improve error handling - Improve error handling
- Add binary release for different platforms and make it available through Homebrew, Winget and Apt - Add binary release for different platforms and make it available through Homebrew, Winget and Apt
- Add due dates and priority levels for todos
- Implement categories/tags for better organization
- Add data export/import functionality

45
main.go
View File

@@ -5,6 +5,7 @@ import (
"flag" "flag"
"fmt" "fmt"
"log" "log"
"os"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
) )
@@ -96,13 +97,41 @@ func removeTodo(db *sql.DB, id int) error {
} }
var ( var (
action = flag.String("action", "add", "action (add, remove, list, complete)") title = flag.String("title", "", "Title of the todo item")
title = flag.String("title", "", "title of the todo") description = flag.String("description", "", "Description of the todo item")
description = flag.String("description", "", "description of the todo") id = flag.Int("id", 0, "ID of the todo item to remove or mark as complete")
id = flag.Int("id", 0, "ID of the todo to be actioned upon. Used for remove and complete")
) )
func init() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Todo CLI - A simple todo list manager\n\n")
fmt.Fprintf(os.Stderr, "Usage:\n %s [action] [flags]\n\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Actions:\n")
fmt.Fprintf(os.Stderr, " add Add a new todo item\n")
fmt.Fprintf(os.Stderr, " remove Remove a todo item\n")
fmt.Fprintf(os.Stderr, " complete Mark a todo item as completed\n")
fmt.Fprintf(os.Stderr, " list List all todo items\n\n")
fmt.Fprintf(os.Stderr, "Flags:\n")
flag.PrintDefaults()
fmt.Fprintf(os.Stderr, "\nExamples:\n")
fmt.Fprintf(os.Stderr, " %s add -t \"Meeting\" -d \"Team standup at 10AM\"\n", os.Args[0])
fmt.Fprintf(os.Stderr, " %s complete -i 1\n", os.Args[0])
fmt.Fprintf(os.Stderr, " %s remove -i 2\n", os.Args[0])
fmt.Fprintf(os.Stderr, " %s list\n", os.Args[0])
}
flag.StringVar(title, "t", "", "shorthand for --title")
flag.StringVar(description, "d", "", "shorthand for --description")
flag.IntVar(id, "i", 0, "shorthand for --id")
}
func main() { func main() {
// Check for help flag before any other operations
if len(os.Args) > 1 && (os.Args[1] == "-h" || os.Args[1] == "--help") {
flag.Usage()
os.Exit(0)
}
db, err := sql.Open("sqlite3", "./todos.db") db, err := sql.Open("sqlite3", "./todos.db")
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
@@ -114,8 +143,14 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
if len(os.Args) < 2 {
flag.Usage()
os.Exit(1)
}
action := os.Args[1]
os.Args = os.Args[1:]
flag.Parse() flag.Parse()
switch *action { switch action {
case "add": case "add":
handleAdd(db) handleAdd(db)
case "remove": case "remove":

BIN
todo

Binary file not shown.