Class CommandLineConfigSource

java.lang.Object
cloud.opencode.base.config.source.CommandLineConfigSource
All Implemented Interfaces:
ConfigSource

public class CommandLineConfigSource extends Object implements ConfigSource
Command Line Arguments Configuration Source 命令行参数配置源

Parses command line arguments into configuration properties with support for multiple formats: --key=value, -Dkey=value, and boolean flags.

将命令行参数解析为配置属性,支持多种格式: --key=value, -Dkey=value 和布尔标志。

Features | 主要功能:

  • Multiple argument formats - 多种参数格式
  • Boolean flag support (--flag -> flag=true) - 布尔标志支持
  • Highest priority (200) - 最高优先级(200)
  • Order preservation - 顺序保留

Usage Examples | 使用示例:

// Parse arguments
String[] args = {
    "--server.port=9090",
    "-Dapp.env=prod",
    "--debug",
    "--database.url=jdbc:mysql://localhost/db"
};

ConfigSource source = new CommandLineConfigSource(args);
// server.port=9090
// app.env=prod
// debug=true
// database.url=jdbc:mysql://localhost/db

Supported Formats | 支持的格式:

--key=value     -> key=value
-Dkey=value     -> key=value
--flag          -> flag=true
-Dflag          -> flag=true

Performance | 性能特性:

  • Time complexity: O(n) for parsing - 时间复杂度: 解析为O(n)
  • Arguments parsed once at creation - 参数在创建时解析一次

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
  • Immutable properties - 不可变属性
Since:
JDK 25, opencode-base-config V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • CommandLineConfigSource

      public CommandLineConfigSource(String[] args)
      Create command line config source 创建命令行配置源
      Parameters:
      args - command line arguments | 命令行参数
  • Method Details

    • getName

      public String getName()
      Description copied from interface: ConfigSource
      Get configuration source name 获取配置源名称
      Specified by:
      getName in interface ConfigSource
      Returns:
      source name | 源名称
    • getProperties

      public Map<String,String> getProperties()
      Description copied from interface: ConfigSource
      Get all configuration properties 获取所有配置属性

      Performance | 性能:

      This method should return a cached immutable map for best performance.

      此方法应返回缓存的不可变映射以获得最佳性能。

      Specified by:
      getProperties in interface ConfigSource
      Returns:
      configuration properties map | 配置属性映射
    • getPriority

      public int getPriority()
      Description copied from interface: ConfigSource
      Get configuration source priority (higher number = higher priority) 获取配置源优先级(数值越大优先级越高)

      Default Priorities | 默认优先级:

      • CommandLine: 200
      • Environment: 100
      • Properties/YAML: 50
      • InMemory: 10
      Specified by:
      getPriority in interface ConfigSource
      Returns:
      priority value | 优先级值