periph-smoketest: various usability improvements

This makes the tool more pleasant to use for newcomers and this makes flag
parsing more coherent.

- Print help on error.
- Consistently use NewFlagSet() everywhere with Usage() that prints nicer help
  page.
- Move code around for the ones that were not layed out in the same way as the
  rest.

This is a breaking change for gpiosmoketest, since it is switched from
undocumented positional arguments to flags.
pull/1/head
Marc-Antoine Ruel 9 years ago
parent dd32a7900e
commit e95c422e15

@ -37,14 +37,14 @@ func (s *SmokeTest) Description() string {
} }
// Run implements the SmokeTest interface. // Run implements the SmokeTest interface.
func (s *SmokeTest) Run(args []string) (err error) { func (s *SmokeTest) Run(f *flag.FlagSet, args []string) (err error) {
f := flag.NewFlagSet(s.Name(), flag.ExitOnError)
i2cID := f.String("i2c", "", "I²C bus to use") i2cID := f.String("i2c", "", "I²C bus to use")
i2cAddr := f.Uint("ia", 0x76, "I²C bus address to use; either 0x76 (BMx280, the default) or 0x77 (BMP180)") i2cAddr := f.Uint("ia", 0x76, "I²C bus address to use; either 0x76 (BMx280, the default) or 0x77 (BMP180)")
spiID := f.String("spi", "", "SPI port to use") spiID := f.String("spi", "", "SPI port to use")
record := f.Bool("r", false, "record operation (for playback unit testing)") record := f.Bool("r", false, "record operation (for playback unit testing)")
f.Parse(args) f.Parse(args)
if f.NArg() != 0 { if f.NArg() != 0 {
f.Usage()
return errors.New("unrecognized arguments") return errors.New("unrecognized arguments")
} }

@ -51,10 +51,8 @@ func (s *SmokeTest) Description() string {
} }
// Run implements the SmokeTest interface. // Run implements the SmokeTest interface.
func (s *SmokeTest) Run(args []string) (err error) { func (s *SmokeTest) Run(f *flag.FlagSet, args []string) (err error) {
s.delay = 2 * time.Second s.delay = 2 * time.Second
f := flag.NewFlagSet(s.Name(), flag.ExitOnError)
i2cID := f.String("i2c", "", "I²C bus to use") i2cID := f.String("i2c", "", "I²C bus to use")
spiID := f.String("spi", "", "SPI port to use") spiID := f.String("spi", "", "SPI port to use")
dcName := f.String("dc", "", "DC pin to use in 4-wire SPI mode") dcName := f.String("dc", "", "DC pin to use in 4-wire SPI mode")
@ -66,6 +64,7 @@ func (s *SmokeTest) Run(args []string) (err error) {
record := f.Bool("record", false, "record operation (for playback unit testing)") record := f.Bool("record", false, "record operation (for playback unit testing)")
f.Parse(args) f.Parse(args)
if f.NArg() != 0 { if f.NArg() != 0 {
f.Usage()
return errors.New("unrecognized arguments") return errors.New("unrecognized arguments")
} }

Loading…
Cancel
Save