Positional arguments and flag package

1,183 views
Skip to first unread message

Amit Saha

unread,
Oct 9, 2020, 12:09:32 AM10/9/20
to golang-nuts
Hi all, I realize that the flag package stops parsing os.Args[] once it finds a non "-" character. This means, if I invoke my program as:

$ ./myprog arg1 -d value

flag.Parse() will stop parsing the moment it sees arg1 and result in NArg() returning 2 instead of 1.

Is there a recommended workaround to handle both $./myprog -d value arg1 and $./myprog arg1 -d value correctly?

Thanks for any suggestions.

Thanks,
Amit.

Roland Müller

unread,
Oct 15, 2020, 2:09:58 AM10/15/20
to Amit Saha, golang-nuts
Actually, it's explicitly documented, so I don't have to guess:


Am Do., 15. Okt. 2020 um 08:22 Uhr schrieb Roland Müller <rol...@gmail.com>:
I guess it's usage of flag is meant in to behave like parsing flags in Unix/LInux, or Python' argparse:
  • after the command you have the flags denoted by '-'
  • after flags follow rest of command line args where elements can be every character string
BR,
Roland

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/d1399079-4589-454f-91d3-820a60aa239dn%40googlegroups.com.

Roland Müller

unread,
Oct 15, 2020, 2:09:58 AM10/15/20
to Amit Saha, golang-nuts
I guess it's usage of flag is meant in to behave like parsing flags in Unix/LInux, or Python' argparse:
  • after the command you have the flags denoted by '-'
  • after flags follow rest of command line args where elements can be every character string
BR,
Roland

Am Fr., 9. Okt. 2020 um 07:10 Uhr schrieb Amit Saha <amits...@gmail.com>:

Amit Saha

unread,
Oct 20, 2020, 12:04:01 AM10/20/20
to Roland Müller, golang-nuts
On Thu, Oct 15, 2020 at 4:27 PM Roland Müller <rol...@gmail.com> wrote:
>
> Actually, it's explicitly documented, so I don't have to guess:
> https://golang.org/pkg/flag/#hdr-Command_line_flag_syntax

Thank you. I read it and it makes sense why the behavior is like that.
Reply all
Reply to author
Forward
0 new messages