periphflag

package module
v0.0.0-...-a1cd721 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 20, 2019 License: Apache-2.0 Imports: 3 Imported by: 1

README

periphflag

This package provides functions for naming hardware buses in flags that produce context-specific usage messages. For example, instead of using a flag.StringVar and getting a usage message like:

$ foo -help
Usage of foo:
  -spi string
        spi bus to use

You will instead get a list of devices that are available:

$ foo -help
Usage of foo:
  -spi string
        spi bus to use; available devices: [FT232H] (default "FT232H")

This should help people get started with your program a bit more easily.

For information about hardware devices, see periph.io.

For full documentation, read the godoc.

Documentation

Overview

Package periphflag integrates periph.io autodiscovery with flags, so that "yourcommand -help" makes more sense to the end user.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SPIDevVar

func SPIDevVar(p *string, name, value, usage string)

SPIDevVar defines a flag that acceps the name of a SPI bus, with some information that is displayed to the end-user replaced with real discovered values. The arguments are the same as a standard flag.StringVar. The default value is looked up against discovered buses and their aliases, and the real device name is substituted. The usage message is changed to have a list of discovered bus names after the usage message.

In order to populate devices, you must have already initialized periph.io with host.Init() or hostextra.Init(). Because you need to check the error from that before proceeding, you will typically not declare these variables at the package level, and instead declare them near the top of your main function:

package main

var (
    someFlag = flag.String("send", "foo", "string to send to the device")
    spi string
)

func main() {
    if _, err := host.Init(); err != nil {
        log.Fatal("periph init: %v", err)
    }
    periphflag.SPIDevVar(&spi, "spi", "", "spi bus to use")
    flag.Parse()

    port, err := spireg.Open(spi)
    if err != nil {
        flag.Usage()
        log.Fatal("open spi port %q: %v", spi, err)
    }
    defer port.Close()
    ...
}

func SPIDevVarOnFlagSet

func SPIDevVarOnFlagSet(f *flag.FlagSet, p *string, name, value, usage string)

SPIDevVarOnFlagSet defines a flag in a manner identical to SPIDevVar on the provided flag.FlagSet.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL