Skip to main content
Version: 2.8.x(Latest)

Introduction

Programs need to manage program start entries through command lines, making command line management components one of the core components of the framework. The GoFrame framework provides a powerful command line management module, implemented by the gcmd component.

Usage:

import "github.com/gogf/gf/v2/os/gcmd"

API Documentation:

https://pkg.go.dev/github.com/gogf/gf/v2/os/gcmd

Features

The gcmd component has the following notable features:

  • Easy to use and powerful
  • Flexible command line parameter management
  • Support for flexible custom parsing through Parser
  • Support for multi-level command line management and richer command line information
  • Structured input/output management for large-scale command lines using object mode
  • Automatic type conversion and validation for structured parameters
  • Ability to read data from configuration components in a structured manner
  • Automatic generation of command line help information
  • Terminal input functionality support

Comparison with Cobra

Cobra is a widely used command line management library in Golang. The open-source project can be found here: https://github.com/spf13/cobra

When compared to Cobra, the gcmd command line component of the GoFrame framework shows similarities in basic functions, but with significant differences in parameter management and observability support:

  • The gcmd component supports structured parameter management with hierarchical object command line management, and method automatic generation without manual definition and parsing of parameter variables by developers.
  • The gcmd component supports automated parameter type conversion for both basic and complex types.
  • The gcmd component provides configurable common parameter validation capabilities to enhance parameter maintenance efficiency.
  • The gcmd component allows reading parameters via configuration components when there are no terminal arguments.
  • The gcmd component supports tracing to facilitate the transfer of trace information between parent and child processes.

Documents

📄️ Command - Methods

Basic functions of the gcmd component in the GoFrame framework, including how to retrieve command line arguments and options and their common methods. Examples demonstrate how to use the Init method to customize command line data, and how to utilize GetArg and GetOpt methods to retrieve command line arguments and options respectively. It details the implementation of argument retrieval and option retrieval, helping developers quickly master command management features in GoFrame.

📄️ Command - Structure

Structured parameter processing for command line management through the GoFrame framework. Manage parent and child commands through object-oriented management, define standardized input parameter objects, and implement automatic data conversion and validation features for command lines. Using GoFrame's framework development tools, users can easily manage multiple command-line projects, support data reading from configurations, and improve project development efficiency and stability.