FAQ
Hi all!
I'm using the great Cobra (https://github.com/spf13/cobra) package to
implement subcommands in my (little) code.
I have two questions, the first is more Cobra-related (I didn'f find a
cobra google group) and the second more general:

First question:
--------------------

* I have (for example) two subcommands A and B and the A subcommand include
some functions and the function called by B
* Thus A and B need to share some flags
* I can't attach the same flags to two different subcommands otherwise my
program will crash with `panic: <B> flag redefined: <flag name>
This is also described in https://code.google.com/p/go/issues/detail?id=298
  and http://golang.org/src/pkg/flag/flag.go

I can agree that using the same flag with different meanings in different
context can be confusing, but the same flag with the same meaning
shouldn't be a problem. Moreover, now I have to put the flags as persistent
and attach them to the root command and I
don't think this is a great solution because I am linking flags with a
command (the root one) that doesn't need them.

Is there a better solution?

Second question:
-------------------------

Why is not possible to check for redefined flags at compile time (given
that they are written in the source) instead of crash
at run time?

thanks a lot

brunetto

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Herbert Fischer at Feb 2, 2014 at 3:54 pm
    Hi brunetto,

    You can send an email directly to the cobra author: https://github.com/spf13

    cheers

    On Sun, Feb 2, 2014 at 11:52 AM, brunetto wrote:

    Hi all!
    I'm using the great Cobra (https://github.com/spf13/cobra) package to
    implement subcommands in my (little) code.
    I have two questions, the first is more Cobra-related (I didn'f find a
    cobra google group) and the second more general:

    First question:
    --------------------

    * I have (for example) two subcommands A and B and the A subcommand
    include some functions and the function called by B
    * Thus A and B need to share some flags
    * I can't attach the same flags to two different subcommands otherwise my
    program will crash with `panic: <B> flag redefined: <flag name>
    This is also described in
    https://code.google.com/p/go/issues/detail?id=298 and
    http://golang.org/src/pkg/flag/flag.go

    I can agree that using the same flag with different meanings in different
    context can be confusing, but the same flag with the same meaning
    shouldn't be a problem. Moreover, now I have to put the flags as
    persistent and attach them to the root command and I
    don't think this is a great solution because I am linking flags with a
    command (the root one) that doesn't need them.

    Is there a better solution?

    Second question:
    -------------------------

    Why is not possible to check for redefined flags at compile time (given
    that they are written in the source) instead of crash
    at run time?

    thanks a lot

    brunetto

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • DisposaBoy at Feb 3, 2014 at 7:26 am
    How is that possible?

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Brunetto at Feb 3, 2014 at 11:36 pm

    On Monday, 3 February 2014 08:26:54 UTC+1, DisposaBoy wrote:
    How is that possible?

    It seems that I made a mistake and if I separate the flags in different
    subcommands it works!
    I'm sorry.

    However I'm still asking myself if it is possible to catch flag errors at
    compile time.

    brunetto


    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedFeb 2, '14 at 1:52p
activeFeb 3, '14 at 11:36p
posts4
users3
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase