liveBook · Manning

Positional parameter

As explained in Go in Practice, Second Edition

A positional argument is a command-line value interpreted according to its position rather than an option name.

A positional parameter is a command-line value interpreted by its location in an argument slice rather than by an option name. Cobra passes command-line arguments to a command handler as []string; extract them explicitly, such as c.Args()[0] for the first value. section 2.1.4

Calculator operands

A calculator command can define its usage as `add <number1> <number2>` and pass args[0] and args[1] to an operation function. The function converts those strings with strconv.Atoi before performing the requested operation. section 2.1.4

Listing 2.8 A basic CLI calculator
package main

import (
    "log"
    "strconv"

    "github.com/spf13/cobra"
)

func operation(op string, n1 string, n2 string) (float32, error) {
    num1, err := strconv.Atoi(n1)
    if err != nil {
        return 0, err
    }
    num2, err := strconv.Atoi(n2)
    if err != nil {
        return 0, err
    }
    fl1 := float32(num1)
    fl2 := float32(num2)
    switch op {
    case "add":
        return fl1 + fl2, nil
    case "sub":
        return fl1 - fl2, nil
    case "mul":
        return fl1 * fl2, nil
    case "div":
        return fl1 / fl2, nil
    }

    return 0, nil
}

var cmdAdd = &cobra.Command{
    Use:   "add",
    Short: "Add two numbers",
    Long:  "Add two numbers: add <number1> <number2>",
    Run: func(cmd *cobra.Command, args []string) {
        result, err := operation("add", args[0], args[1])
        if err != nil {
            log.Fatal(err)
        }
        log.Printf("result: %f\n", result)
    },
    Args: cobra.ExactArgs(2),
}

var cmdSub = &cobra.Command{
    Use:   "sub",
    Short: "Subtract two numbers",
    Long:  "Subtract two numbers: sub <number1> <number2>",
    Run: func(cmd *cobra.Command, args []string) {
        result, err := operation("sub", args[0], args[1])
        if err != nil {
            log.Fatal(err)
        }
        log.Printf("result: %f\n", result)
    },
    Args: cobra.ExactArgs(2),
}

var cmdMul = &cobra.Command{
    Use:   "mul",
    Short: "Multiply two numbers",
    Long:  "Multiply two numbers: mul <number1> <number2>",
    Run: func(cmd *cobra.Command, args []string) {
        result, err := operation("mul", args[0], args[1])
        if err != nil {
            log.Fatal(err)
        }
        log.Printf("result: %f\n", result)
    },
    Args: cobra.ExactArgs(2),
}

var cmdDiv = &cobra.Command{
    Use:   "div",
    Short: "Divide two numbers",
    Long:  "Divide two numbers: div <number1> <number2>",
    Run: func(cmd *cobra.Command, args []string) {
        result, err := operation("div", args[0], args[1])
        if err != nil {
            log.Fatal(err)
        }
        log.Printf("result: %f\n", result)
    },
    Args: cobra.ExactArgs(2),
}

func main() {
var calculator = &cobra.Command{Use: "calculator", Short: "A simple 
calculator"}
    calculator.AddCommand(cmdAdd)
    calculator.AddCommand(cmdSub)
    calculator.AddCommand(cmdMul)
    calculator.AddCommand(cmdDiv)
    calculator.Execute()
}

Inspect the calculator implementation to see positional operands flow from Cobra's args slice into arithmetic processing. section 2.1.4

How Go in Practice, Second Edition covers it

How deep it goes
Defines extraction and demonstrates two operands, but does not detail validation or argument-count handling.
Examples
Demonstrates first-argument extraction and an add command receiving number1 and number2 as positional operands.
What it assumes
Assumes basic command-line and Go knowledge while clarifying that arguments require explicit extraction before use.
What you can do after
Apply positional arguments by extracting values from Cobra's command argument slice and passing them to application logic.
How it connects
Connects positional arguments with Cobra, command-line interfaces, flags, commands, and subcommands.

Also explained in

Compare how 5 books explain positional parameter →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go in Practice, Second Edition. The section links point to the parts of the book it draws on.