From 6f8cfa703a6b5eabeef4b957d14cfc200a16c908 Mon Sep 17 00:00:00 2001 From: Audrius Butkevicius Date: Sun, 13 Jul 2014 15:18:45 +0100 Subject: [PATCH] Add Args.Swap command for swapping arguments The usecase: my-cli user set Being able to swap with set argument, we can have nested subcommands while preserving all variable arguments along the way. --- context.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/context.go b/context.go index b2c51bb..1e023ce 100644 --- a/context.go +++ b/context.go @@ -140,6 +140,15 @@ func (a Args) Present() bool { return len(a) != 0 } +// Swaps arguments at the given indexes +func (a Args) Swap(from, to int) error { + if from >= len(a) || to >= len(a) { + return errors.New("index out of range") + } + a[from], a[to] = a[to], a[from] + return nil +} + func lookupInt(name string, set *flag.FlagSet) int { f := set.Lookup(name) if f != nil {