Basic CLI tool
Learn how to create a basic CLI tool
go python
Exercise
To test out the the CLI letβs get the CLI returning the ASCII art logo of envtamer and the tagline when invoking the CLI without any arguments.
ASCI-art
___ _ __ __ __ | |_ __ _ _ __ ___ ___ _ __
/ _ \ | '_ \ \ \ / / | __| / _' | | '_ ' _ \ / _ \ | '__|
| __/ | | | | \ V / | |_ | (_| | | | | | | | | __/ | |
\___| |_| |_| \_/ \__| \__,_| |_| |_| |_| \___| |_|
Tagline short
Taming digital environment files chaos with elegant simplicity
Tagline long
A command-line tool for managing environment variables across different projects and directories.
Go
Python
Solution
Go
cmd/envtamer/main.go
package main
import (
"fmt"
"os"
"github.com/mlouage/envtamer-go/internal/command"
)
func main() {
rootCmd := command.NewRootCmd()
if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
} internal/command/root.go
package command
import (
"fmt"
"github.com/spf13/cobra"
)
const logo = `
___ _ __ __ __ | |_ __ _ _ __ ___ ___ _ __
/ _ \ | '_ \ \ \ / / | __| / _' | | '_ ' _ \ / _ \ | '__|
| __/ | | | | \ V / | |_ | (_| | | | | | | | | __/ | |
\___| |_| |_| \_/ \__| \__,_| |_| |_| |_| \___| |_|
`
// NewRootCmd creates the root command
func NewRootCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "envtamer",
Short: "Taming digital environment files chaos with elegant simplicity",
Long: fmt.Sprintf("%s\nA command-line tool for managing environment variables across different projects and directories.", logo),
}
return cmd
}
Python
cli.py
import click
ascii_art = r"""
___ _ __ __ __ | |_ __ _ _ __ ___ ___ _ __
/ _ \ | '_ \ \ \ / / | __| / _' | | '_ ' _ \ / _ \ | '__|
| __/ | | | | \ V / | |_ | (_| | | | | | | | | __/ | |
\___| |_| |_| \_/ \__| \__,_| |_| |_| |_| \___| |_|
"""
print(ascii_art)
@click.Group
def cli():
pass
if __name__ == '__main__':
cli()
pyproject.toml
[project]
name = "envtamer"
version = "0.1.0"
description = ""
authors = [
{name = "Stefan van Tilborg",email = "stefan@xprtz.net"}
]
readme = "README.md"
requires-python = ">=3.13"
dependencies = [
"click (>=8.1.8,<9.0.0)"
]
packages = [
{ include = "*.py" },
{ include = "envtamer" },
{ include = "envtamer/*.py" }
]
[tool.poetry.scripts]
envtamer = "envtamer.cli:cli"
[build-system]
requires = ["poetry-core>=2.0.0,<3.0.0"]
build-backend = "poetry.core.masonry.api"