Code Monkey home page Code Monkey logo

Comments (1)

DavidVorick avatar DavidVorick commented on May 21, 2024

for the sake of completeness, here is the full output:

Usage of server:

Usage: 
  server [flags]
  server [command]

Available Commands: 
  version                   Print version information
  help [command]            Help about any command

 Available Flags:
  -c, --config="": Where to find the server configuration file.
      --help=false: help for server

Use "server help [command]" for more information about that command.

And here is the code I wrote to get that output:

package main

import (
    "fmt"

    "github.com/NebulousLabs/Sia/siafiles"

    "github.com/spf13/cobra"
)

// defaultConfigLocation checks a bunch of places for the config file, in a
// particular order, and then returns the first one found.
func defaultConfigLocation() (configLocation string) {
    // Check the home directory for a config file.
    homeLocation, err := siafiles.HomeFilename("config-server")
    if err != nil {
        // Log something, but somewhere that normal users will never see.
    } else {
        if siafiles.Exists(homeLocation) {
            configLocation = homeLocation
            return
        }
    }

    // Check the /etc directory for a config file... not sure what the
    // windows equivalent would be.
    rootLocation := "/etc/siaserver-config"
    if siafiles.Exists(rootLocation) {
        configLocation = rootLocation
        return
    }

    return
}

func start(cmd *cobra.Command, args []string) {
    fmt.Println("Hello World!")
}

func version(cmd *cobra.Command, args []string) {
    fmt.Println("Sia Server v0.0.3")
}

func main() {
    root := &cobra.Command{
        Use:   "server",
        Short: "Sia server",
        Long:  "Sia server, meant to run in the background and interface with a client.",
        Run:   start,
    }

    dcl := defaultConfigLocation()
    var configLocation string
    root.Flags().StringVarP(&configLocation, "config", "c", dcl, "Where to find the server configuration file.")

    version := &cobra.Command{
        Use:   "version",
        Short: "Print version information",
        Run:   version,
    }

    root.AddCommand(version)

    root.Execute()
}

from cobra.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.