chore: add version flag

This commit is contained in:
2024-09-03 01:00:52 +03:00
parent 798c27a763
commit 17611568eb
3 changed files with 35 additions and 12 deletions

View File

@@ -8,7 +8,7 @@ from a file, standard input, or command-line arguments.
## Usage
```sh
treelike [OPTIONS] [PATH]
treelike [OPTIONS] [TREE-STRUCTURE]
```
Prints a tree-like representation of the input.
@@ -16,6 +16,7 @@ Prints a tree-like representation of the input.
### Options
- `-h, --help`: Show help message and exit.
- `-V, --version`: Show the version number and exit.
- `-f, --file FILE`: Read from FILE.
- `-, --stdin`: Read from stdin.
- `-c, --charset CHARSET`: Use CHARSET to display characters (utf-8, ascii).
@@ -28,19 +29,25 @@ Prints a tree-like representation of the input.
### From Releases
1. Go to the [Releases](https://github.com/chenasraf/treelike/releases) page.
2. Download the appropriate binary for your platform:
- **Windows**: `treelike-windows-amd64.exe`
- **macOS**: `treelike-darwin-amd64`
- **Linux**: `treelike-linux-amd64`
3. Make the binary executable (if necessary):
- **Windows**: `treelike-windows-amd64.tar.gz`
- **macOS**: `treelike-darwin-amd64.tar.gz`
- **Linux**: `treelike-linux-amd64.tar.gz`
3. Extract the tar:
```sh
chmod +x treelike-darwin-amd64 # For macOS
chmod +x treelike-linux-amd64 # For Linux
tar -xzf treelike-darwin-amd64.tar.gz # macOS
tar -xzf treelike-linux-amd64.tar.gz # Linux
```
4. Move the binary to a directory in your PATH:
4. Make the binary executable (if necessary):
```sh
mv treelike-darwin-amd64 /usr/local/bin/treelike # For macOS
mv treelike-linux-amd64 /usr/local/bin/treelike # For Linux
chmod +x treelike
```
5. Move the binary to a directory in your PATH:
```sh
mv treelike /usr/local/bin/treelike
```
### From Source

17
args.go
View File

@@ -1,11 +1,15 @@
package main
import (
"embed"
"fmt"
"os"
"strings"
)
//go:embed version.txt
var VERSION embed.FS
// getOpts parses command-line arguments and returns an Options struct populated with the parsed values.
// It supports various flags to customize the behavior of the program, such as reading from stdin,
// specifying a file, setting the charset, and more. If an invalid charset is provided, the function
@@ -31,7 +35,18 @@ func getOpts() Options {
switch args[0] {
case "-h", "--help":
{
helpText()
help := helpText()
fmt.Printf("%s", help.String())
os.Exit(0)
}
case "-V", "--version":
{
version, err := VERSION.ReadFile("version.txt")
if err != nil {
fmt.Println("Error getting version:", err)
os.Exit(0)
}
fmt.Println(string(version))
os.Exit(0)
}
case "-", "--stdin":

View File

@@ -14,11 +14,12 @@ import (
func helpText() strings.Builder {
LE := getLE()
var builder strings.Builder
builder.WriteString("Usage: treelike [OPTIONS] [PATH]" + LE)
builder.WriteString("Usage: treelike [OPTIONS] [TREE-STRUCTURE]" + LE)
builder.WriteString("Prints a tree-like representation of the input." + LE)
builder.WriteString("" + LE)
builder.WriteString("Options:" + LE)
builder.WriteString(" -h, --help Show this help message and exit" + LE)
builder.WriteString(" -V, --version Show the version number and exit" + LE)
builder.WriteString(" -f, --file FILE Read from FILE" + LE)
builder.WriteString(" -, --stdin Read from stdin" + LE)
builder.WriteString(" -c, --charset CHARSET Use CHARSET to display characters (utf-8, ascii)" + LE)