Bash Terminal

I have been playing around with GO for a while. I really like the language and I am slowly getting an understanding of the language syntax by building trivial apllications. These applications end up being simple command line tools and need some way off parsing command line arguments.

This GO Package was created to make it easy to add simple command-line interfaces, where the application can define what arguments are requires and pair the argument with an information message. This array of arguments are passed into the goArgs object which then can be used to parse the arguments and provide a help / usage message on any invalid arguments.

At somepoint I may create a more fully featured parser, though I'm not sure when I will get round to buliding it.

Command are identified by a '--' prefix, the associated argument is the value directly after.

The command will be identified as "count" and the folder is identified as the "./" in the below example.
> goargsexample --command count --folder ./
>
> Folder contains 10 files.
The argument usage message can be shown using the '--help' or '-h' command.
> goargsexample --help
>
> Help
>
> --command <list, count, sum>
> --folder <folder path>
The below code is a trivial GO program that takes a command value and folder path,
it then prints the requested result to the screen.
package main

// Import the packages
import (
  "fmt"
  "github.com/bertmcdowell/goargs/args"
  "os"
)

// Build the command line argument list and help string
var commands = map[string]string{
  "command": "list, count, sum",
  "folder":  "folder path",
}

// Main Function
func main() {

  // Create and Parse the command line arguments
  args := args.CreateWithOSArgs(commands)

  // Print an empty line
  fmt.Println("")

  // Check to see if we have an error
  if args.HasError() {
    // Print the error and usage information
    args.PrintUsageAndErrors()
  } else {

    // Check to see if the paramters required are present
    if !args.HasParam("command") || !args.HasParam("folder") {
      // Commands parameters are missing display the usage message
      args.PrintUsageAndErrors()
    } else {

      commandName := args.GetParam("command")
      folderPath := args.GetParam("folder")

      info, err := os.Stat(folderPath)

      if err != nil {
        fmt.Println(err)
      } else {

        if !info.IsDir() {
          fmt.Println("Path is not a directory.")
        } else {
          // Open the directory File object
          d, err := os.Open(folderPath)
          if err != nil {
            fmt.Println(err)
          } else {
            // Read the directory contents
            fi, err := d.Readdir(-1)
            if err != nil {
              fmt.Println(err)
            } else {
              // Switch on the command passed in
              switch commandName {
              case "list":
                for _, fi := range fi {
                  if fi.Mode().IsRegular() {
                    fmt.Println(fi.Name())
                  }
                }

              case "count":
                fmt.Println("Folder contains ", len(fi), " files.")

              case "sum":
                var bytes int64
                for _, fi := range fi {
                  if fi.Mode().IsRegular() {
                    bytes += fi.Size()
                  }
                }
                fmt.Println("Folder contains ", bytes, " bytes with of files.")

              }
            }
            // Close the directory File object
            d.Close()
          }
        }
      }
    }
  }
}

All code is supplied as is an use at your own risk.

Comments

Comments powered by Disqus