Command Line Argument Processing in Go: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
(Created page with "=Internal= *Go Code Examples#Code_Examples =Overview= <font color=darkkhaki>Use the <code>flag</code> package: </font> <syntaxhighlight lang='go'> var port int flag.IntV...")
 
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
=Internal=
=Internal=
*[[Go Code Examples#Code_Examples]]
*[[Go Code Examples#Code_Examples|Go Code Examples]]
* [[Go Package flag]]
 
=Overview=
=Overview=


<font color=darkkhaki>Use the <code>flag</code> package: </font>
<font color=darkkhaki>Use the <code>[[Go Package flag|flag]]</code> package: </font>
<syntaxhighlight lang='go'>
<syntaxhighlight lang='go'>


Line 10: Line 12:
flag.Parse()
flag.Parse()
</syntaxhighlight>
</syntaxhighlight>
<font color=darkkhaki>I didn't find a way to tel whether the value has been indeed provided on command line or it's the default. This can be a problem when we also read the value from a configuration file, and we want the priority to be, in order: command line, config file and default</font>.
=Positional Arguments=
<syntaxhighlight lang='go'>
args := os.Args
</syntaxhighlight>
<code>args[0]</code> is a string that represents the path of the executable, as it was invoked on the command line. Example: <code>./../bin/metadata</code>.
=TO DEPLETE=
<font color=darkkhaki>TO DEPLETE [[Go Command Line Parsing]]</font>

Latest revision as of 02:31, 18 October 2023

Internal

Overview

Use the flag package:

var port int
flag.IntVar(&port, "port", 8080, "Some documentation for the flag")
flag.Parse()

I didn't find a way to tel whether the value has been indeed provided on command line or it's the default. This can be a problem when we also read the value from a configuration file, and we want the priority to be, in order: command line, config file and default.

Positional Arguments

args := os.Args

args[0] is a string that represents the path of the executable, as it was invoked on the command line. Example: ./../bin/metadata.

TO DEPLETE

TO DEPLETE Go Command Line Parsing