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 - Concepts
Basic concepts of command line arguments and options in the GoFrame framework, discussing the definition and distinction of parameters and options and how they are represented in the command line. It explains in detail the parsing rules of options and parameters, including the use of the equal sign to connect options and data. Also provides implementation details and usage examples of the gcmd component in GoFrame to help users better manage command line inputs.
📄️ 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 - Args Parsing
The command line parsing feature of the GoFrame framework, focusing on the Parser parsing method of the gcmd component. By customizing option names and value parsing, it can efficiently manage and parse command line options. Includes code examples and detailed API documentation to help developers understand and apply it in actual projects.
📄️ Command - Object
Manage command line objects and commands using the gcmd library in the GoFrame framework, covering the definition of Command object, the use of callback methods, and hierarchical management of commands. It also provides examples of starting HTTP and gRPC services via command line within the GoFrame framework, demonstrating how to add subcommands to commands and automatically generate help information.
📄️ 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.
📄️ Command - Interaction
The gcmd component in the GoFrame framework is used for interacting with users in a terminal environment. It primarily offers two core methods, Scan and Scanf, which facilitate convenient reading of user input from the terminal and interactive display in the command line interface. These practical features are suitable for the development of command-line programs that require interaction with user data input.
📄️ Command - Tracing
Using the command management component of the GoFrame framework for tracing. Through the OpenTelemetry specification, GoFrame can achieve cross-process tracing, which is especially suitable for processes that run temporarily. The example demonstrates how to call sub-processes from the main process and automatically pass link information.