commit c2d394dbee97af98114b5f5552cd411442b06c51 Author: Alexander Zinn Date: Sat Sep 6 21:33:15 2025 -0400 initial commit diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..e69de29 diff --git a/cmd/root.go b/cmd/root.go new file mode 100644 index 0000000..0cd1b16 --- /dev/null +++ b/cmd/root.go @@ -0,0 +1,51 @@ +/* +Copyright © 2025 NAME HERE + +*/ +package cmd + +import ( + "os" + + "github.com/spf13/cobra" +) + + + +// rootCmd represents the base command when called without any subcommands +var rootCmd = &cobra.Command{ + Use: "mytextfmt", + Short: "A brief description of your application", + Long: `A longer description that spans multiple lines and likely contains +examples and usage of using your application. For example: + +Cobra is a CLI library for Go that empowers applications. +This application is a tool to generate the needed files +to quickly create a Cobra application.`, + // Uncomment the following line if your bare application + // has an action associated with it: + // Run: func(cmd *cobra.Command, args []string) { }, +} + +// Execute adds all child commands to the root command and sets flags appropriately. +// This is called by main.main(). It only needs to happen once to the rootCmd. +func Execute() { + err := rootCmd.Execute() + if err != nil { + os.Exit(1) + } +} + +func init() { + // Here you will define your flags and configuration settings. + // Cobra supports persistent flags, which, if defined here, + // will be global for your application. + + // rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.mytextfmt.yaml)") + + // Cobra also supports local flags, which will only run + // when this action is called directly. + rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} + + diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..e836018 --- /dev/null +++ b/go.mod @@ -0,0 +1,9 @@ +module mytextfmt + +go 1.25.1 + +require ( + github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/spf13/cobra v1.10.1 // indirect + github.com/spf13/pflag v1.0.10 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..989827e --- /dev/null +++ b/go.sum @@ -0,0 +1,11 @@ +github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g= +github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/spf13/cobra v1.10.1 h1:lJeBwCfmrnXthfAupyUTzJ/J4Nc1RsHC/mSRU2dll/s= +github.com/spf13/cobra v1.10.1/go.mod h1:7SmJGaTHFVBY0jW4NXGluQoLvhqFQM+6XSKD+P4XaB0= +github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk= +github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go new file mode 100644 index 0000000..b1624a7 --- /dev/null +++ b/main.go @@ -0,0 +1,99 @@ +/* +Copyright © 2025 NAME HERE +*/ +package main + +import ( + "bufio" + "fmt" + "os" + "strings" + + "github.com/spf13/cobra" +) + +var ( + uppercase bool + lowercase bool + titlecase bool + wordcount bool + charcount bool +) + +var rootCmd = &cobra.Command{ + Use: "mytextfmt [text]", + Short: "A simple text formatter", + Long: `mytextfmt is a CLI tool for formatting and analyzing text. + +You can provide text as arguments or pipe it in via stdin. +Multiple formatting options can be applied simultaneously`, + Args: cobra.ArbitraryArgs, + Run: runTextFormatter, +} + +func init() { + rootCmd.Flags().BoolVarP(&uppercase, "upper", "u", false, "Convert text to uppercase") + rootCmd.Flags().BoolVarP(&lowercase, "lower", "l", false, "Convert text to lowercase") + rootCmd.Flags().BoolVarP(&titlecase, "title", "t", false, "Convert text to titlecase") + rootCmd.Flags().BoolVar(&wordcount, "words", false, "Count words in text") + rootCmd.Flags().BoolVar(&charcount, "chars", false, "Count characters in text") +} + +func runTextFormatter(cmd *cobra.Command, args []string) { + var text string + + // Get input text from args or stdin + if len(args) > 0 { + text = strings.Join(args, " ") + } else { + scanner := bufio.NewScanner(os.Stdin) + var lines []string + + for scanner.Scan() { + lines = append(lines, scanner.Text()) + } + + text = strings.Join(lines, "\n") + } + + if text == "" { + fmt.Println("No input text was provided") + + return + } + + result := text + + if uppercase { + result = strings.ToUpper(result) + } + + if lowercase { + result = strings.ToLower(result) + } + + if titlecase { + result = strings.Title(result) + } + + fmt.Println(result) + + if wordcount { + words := len(strings.Fields(text)) + + fmt.Printf("Word count [%d]\n", words) + } + + if charcount { + chars := len(text) + + fmt.Printf("Character count [%d]\n", chars) + } +} + +func main() { + if err := rootCmd.Execute(); err != nil { + fmt.Println(err) + os.Exit(1) + } +} diff --git a/mytextfmt b/mytextfmt new file mode 100755 index 0000000..24ccd46 Binary files /dev/null and b/mytextfmt differ