github.com/richardwilkes/toolbox@v1.121.0/cmdline/options.go (about) 1 // Copyright (c) 2016-2024 by Richard A. Wilkes. All rights reserved. 2 // 3 // This Source Code Form is subject to the terms of the Mozilla Public 4 // License, version 2.0. If a copy of the MPL was not distributed with 5 // this file, You can obtain one at http://mozilla.org/MPL/2.0/. 6 // 7 // This Source Code Form is "Incompatible With Secondary Licenses", as 8 // defined by the Mozilla Public License, version 2.0. 9 10 package cmdline 11 12 import ( 13 "unicode" 14 ) 15 16 // Options represents a set of options. 17 type Options []*Option 18 19 // Len implements the sort.Interface interface. 20 func (op Options) Len() int { 21 return len(op) 22 } 23 24 // Less implements the sort.Interface interface. 25 func (op Options) Less(i, j int) bool { 26 in := op[i].single 27 jn := op[j].single 28 if in == 0 && jn != 0 { 29 return false 30 } 31 if in != 0 && jn == 0 { 32 return true 33 } 34 in = swapCase(in) 35 jn = swapCase(jn) 36 if in < jn { 37 return true 38 } 39 if in == jn { 40 return op[i].name < op[j].name 41 } 42 return false 43 } 44 45 // Swap implements the sort.Interface interface. 46 func (op Options) Swap(i, j int) { 47 op[i], op[j] = op[j], op[i] 48 } 49 50 func swapCase(ch rune) rune { 51 if unicode.IsUpper(ch) { 52 return unicode.ToLower(ch) 53 } 54 if unicode.IsLower(ch) { 55 return unicode.ToUpper(ch) 56 } 57 return ch 58 }