pkg.re/essentialkaos/ek.10@v12.41.0+incompatible/usage/example_test.go (about)

     1  package usage
     2  
     3  // ////////////////////////////////////////////////////////////////////////////////// //
     4  //                                                                                    //
     5  //                         Copyright (c) 2022 ESSENTIAL KAOS                          //
     6  //      Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0>     //
     7  //                                                                                    //
     8  // ////////////////////////////////////////////////////////////////////////////////// //
     9  
    10  import (
    11  	"fmt"
    12  )
    13  
    14  // ////////////////////////////////////////////////////////////////////////////////// //
    15  
    16  func ExampleAbout_Render() {
    17  	about := About{
    18  		App:     "MySupperApp",
    19  		Desc:    "My super golang utility",
    20  		Version: "1.0.1",
    21  		Release: "-44",
    22  		Build:   "17746", // Number of build or commit hash
    23  		Year:    2009,    // Year when company was founded
    24  		License: "MIT",
    25  		Owner:   "John Dow <john@domain.com>",
    26  
    27  		AppNameColorTag: "{r*}", // Use custom color for application name
    28  		VersionColorTag: "{r}",  // Use custom color for application version
    29  	}
    30  
    31  	about.Render()
    32  }
    33  
    34  func ExampleNewInfo() {
    35  	// If the first argument (name) is empty, we use the name of the file
    36  	// for info generation
    37  	info := NewInfo("")
    38  
    39  	// You can hardcode the name of the app if you want
    40  	info = NewInfo("myapp")
    41  
    42  	// You can customize some colors
    43  	info.AppNameColorTag = "{c}"
    44  	info.CommandsColorTag = "{y}"
    45  	info.OptionsColorTag = "{m}"
    46  
    47  	// You can define one or more arguments handled by your program
    48  	info = NewInfo("", "files…")
    49  	info = NewInfo("", "input", "num-files", "output")
    50  
    51  	info.Render()
    52  }
    53  
    54  func ExampleInfo_AddGroup() {
    55  	info := NewInfo("", "items…")
    56  
    57  	// You can add custom commands groups
    58  	info.AddGroup("External Commands")
    59  
    60  	// ... and add commands to this group
    61  	info.AddCommand("publish", "Publish items")
    62  
    63  	// You can define option (output) and payload (file) name
    64  	info.AddOption("o:output", "Output", "file")
    65  
    66  	// render all data
    67  	info.Render()
    68  }
    69  
    70  func ExampleInfo_AddCommand() {
    71  	info := NewInfo("", "items…")
    72  
    73  	// You can define command arguments names
    74  	info.AddCommand("add", "Add item", "file")
    75  
    76  	// Also, you can mark optional arguments using ? prefix
    77  	info.AddCommand("remove", "Remove item", "file", "?mode")
    78  	info.AddCommand("list", "List items")
    79  
    80  	// You can add custom commands groups
    81  	info.AddGroup("External Commands")
    82  
    83  	info.AddCommand("publish", "Publish items")
    84  
    85  	// render all data
    86  	info.Render()
    87  }
    88  
    89  func ExampleInfo_AddOption() {
    90  	info := NewInfo("", "items…")
    91  
    92  	// AddOption supports options in format used in options package
    93  	info.AddOption("v:version", "Print version")
    94  
    95  	// You can define option (output) and payload (file) name
    96  	info.AddOption("o:output", "Output", "file")
    97  
    98  	// render all data
    99  	info.Render()
   100  }
   101  
   102  func ExampleInfo_AddExample() {
   103  	info := NewInfo("", "items…")
   104  
   105  	info.AddCommand("add", "Add item", "file")
   106  	info.AddCommand("remove", "Remove item", "file", "?mode")
   107  
   108  	// First part with application name will be automatically added
   109  	info.AddExample("add file.dat")
   110  
   111  	// This is example with description
   112  	info.AddExample("remove file.dat", "Remove file.dat")
   113  
   114  	// render all data
   115  	info.Render()
   116  }
   117  
   118  func ExampleInfo_AddRawExample() {
   119  	info := NewInfo("", "items…")
   120  
   121  	info.AddCommand("add", "Add item", "file")
   122  	info.AddCommand("remove", "Remove item", "file", "?mode")
   123  
   124  	// Raw example (without application name) without description
   125  	info.AddRawExample("add file.dat")
   126  
   127  	// Raw example (without application name) with description
   128  	info.AddRawExample("remove file.dat", "Remove file.dat")
   129  
   130  	// render all data
   131  	info.Render()
   132  }
   133  
   134  func ExampleInfo_AddSpoiler() {
   135  	info := NewInfo("", "items…")
   136  
   137  	// Spoiler will be shown before all commands and options
   138  	info.AddSpoiler("This is my supadupa utility")
   139  
   140  	// render all data
   141  	info.Render()
   142  }
   143  
   144  func ExampleInfo_BoundOptions() {
   145  	info := NewInfo("", "items…")
   146  
   147  	info.AddCommand("publish", "Publish items")
   148  
   149  	info.AddOption("o:output", "Output", "file")
   150  
   151  	// Link command and options (will be used for completion generation)
   152  	info.BoundOptions("publish", "o:output")
   153  
   154  	// render all data
   155  	info.Render()
   156  }
   157  
   158  func ExampleInfo_GetCommand() {
   159  	info := NewInfo("", "items…")
   160  
   161  	// You can define command arguments names
   162  	info.AddCommand("add", "Add item", "file")
   163  
   164  	// Also, you can mark optional arguments using ? prefix
   165  	info.AddCommand("remove", "Remove item", "file", "?mode")
   166  	info.AddCommand("list", "List items")
   167  
   168  	cmd := info.GetCommand("list")
   169  
   170  	fmt.Println(cmd.Desc)
   171  	// Output: List items
   172  }
   173  
   174  func ExampleInfo_GetOption() {
   175  	info := NewInfo("", "items…")
   176  
   177  	// AddOption supports options in format used in options package
   178  	info.AddOption("v:version", "Print version")
   179  
   180  	// You can define option argument name
   181  	info.AddOption("o:output", "Output file", "file")
   182  
   183  	opt := info.GetOption("o:output")
   184  
   185  	fmt.Println(opt.Desc)
   186  	// Output: Output file
   187  }
   188  
   189  func ExampleInfo_Render() {
   190  	info := NewInfo("", "items…")
   191  
   192  	// Spoiler will be shown before all commands and options
   193  	info.AddSpoiler("This is my supadupa utility")
   194  
   195  	// You can define command arguments names
   196  	info.AddCommand("add", "Add item", "file")
   197  
   198  	// Also, you can mark optional arguments using ? prefix
   199  	info.AddCommand("remove", "Remove item", "file", "?mode")
   200  	info.AddCommand("list", "List items")
   201  
   202  	// You can add custom commands groups
   203  	info.AddGroup("External Commands")
   204  
   205  	info.AddCommand("publish", "Publish items")
   206  
   207  	info.AddOption("--help", "Print help content")
   208  
   209  	// AddOption supports options in format used in options package
   210  	info.AddOption("v:version", "Print version")
   211  
   212  	// You can define option argument name
   213  	info.AddOption("o:output", "Output", "file")
   214  
   215  	// Link command and options (will be used for completion generation)
   216  	info.BoundOptions("publish", "o:output")
   217  
   218  	// First part with application name will be automatically added
   219  	info.AddExample("add file.dat")
   220  
   221  	// This is example with description
   222  	info.AddExample("remove file.dat", "Remove file.dat")
   223  
   224  	// Raw example without description
   225  	info.AddRawExample("add file.dat")
   226  
   227  	// Raw example with description
   228  	info.AddRawExample("remove file.dat", "Remove file.dat")
   229  
   230  	// render all data
   231  	info.Render()
   232  }