C #에서 명령 줄 인수를 구문 분석하는 가장 좋은 방법은 무엇입니까? [닫은]
매개 변수를 사용하는 콘솔 애플리케이션을 빌드 할 때에 전달 된 인수를 사용할 수 있습니다 Main(string[] args)
.
과거에는 단순히 해당 배열을 인덱싱 / 루프하고 값을 추출하기 위해 몇 가지 정규식을 수행했습니다. 그러나 명령이 더 복잡해지면 구문 분석이 매우 추악해질 수 있습니다.
그래서 관심이 있습니다.
- 사용하는 라이브러리
- 사용하는 패턴
명령이 항상 여기에 답변 된 것과 같은 공통 표준을 준수한다고 가정합니다 .
NDesk.Options ( Documentation ) 및 / 또는 Mono.Options (동일한 API, 다른 네임 스페이스)를 사용하는 것이 좋습니다 . 문서 의 예 :
bool show_help = false;
List<string> names = new List<string> ();
int repeat = 1;
var p = new OptionSet () {
{ "n|name=", "the {NAME} of someone to greet.",
v => names.Add (v) },
{ "r|repeat=",
"the number of {TIMES} to repeat the greeting.\n" +
"this must be an integer.",
(int v) => repeat = v },
{ "v", "increase debug message verbosity",
v => { if (v != null) ++verbosity; } },
{ "h|help", "show this message and exit",
v => show_help = v != null },
};
List<string> extra;
try {
extra = p.Parse (args);
}
catch (OptionException e) {
Console.Write ("greet: ");
Console.WriteLine (e.Message);
Console.WriteLine ("Try `greet --help' for more information.");
return;
}
저는 Command Line Parser Library ( http://commandline.codeplex.com/ )를 정말 좋아합니다 . 속성을 통해 매개 변수를 설정하는 매우 간단하고 우아한 방법이 있습니다.
class Options
{
[Option("i", "input", Required = true, HelpText = "Input file to read.")]
public string InputFile { get; set; }
[Option(null, "length", HelpText = "The maximum number of bytes to process.")]
public int MaximumLenght { get; set; }
[Option("v", null, HelpText = "Print details during execution.")]
public bool Verbose { get; set; }
[HelpOption(HelpText = "Display this help screen.")]
public string GetUsage()
{
var usage = new StringBuilder();
usage.AppendLine("Quickstart Application 1.0");
usage.AppendLine("Read user manual for usage instructions...");
return usage.ToString();
}
}
WPF TestApi 라이브러리는 C #을 개발을위한 좋은 명령 줄 파서 중 하나와 함께 제공됩니다. API에 대한 Ivo Manolov의 블로그 에서 살펴 보는 것이 좋습니다 .
// EXAMPLE #2:
// Sample for parsing the following command-line:
// Test.exe /verbose /runId=10
// This sample declares a class in which the strongly-
// typed arguments are populated
public class CommandLineArguments
{
bool? Verbose { get; set; }
int? RunId { get; set; }
}
CommandLineArguments a = new CommandLineArguments();
CommandLineParser.ParseArguments(args, a);
http://github.com/mono/mono/tree/master/mcs/class/Mono.Options/를 보십시오.
모든 사람들이 자신의 애완 동물 명령 줄 파서를 가지고있는 것 같습니다.
이 라이브러리에는 명령 줄 의 값으로 클래스를 초기화 하는 명령 줄 구문 분석기 가 포함되어 있습니다 . 그것은 많은 기능을 가지고 있습니다 (나는 수년에 걸쳐 그것을 구축해 왔습니다).
로부터 문서 ...
BizArk 프레임 워크의 명령 줄 구문 분석에는 다음과 같은 주요 기능이 있습니다.
- 자동 초기화 : 클래스 속성은 명령 줄 인수에 따라 자동으로 설정됩니다.
- 기본 속성 : 속성 이름을 지정하지 않고 값을 보냅니다.
- 값 변환 : BizArk에 포함 된 강력한 ConvertEx 클래스를 사용하여 값을 적절한 유형으로 변환합니다.
- 부울 플래그 : 플래그는 단순히 인수 (예 : true의 경우 / b, false의 경우 / b-)를 사용하거나 true / false, yes / no 등의 값을 추가하여 지정할 수 있습니다.
- 인수 배열 : 명령 줄 이름 뒤에 여러 값을 추가하여 배열로 정의 된 속성을 설정하기 만하면됩니다. 예를 들어, / x 1 2 3은 x를 {1, 2, 3} 배열로 채 웁니다 (x가 정수 배열로 정의되었다고 가정).
- 명령 줄 별칭 : 속성은 여러 명령 줄 별칭을 지원할 수 있습니다. 예를 들어, 도움말은?라는 별칭을 사용합니다.
- 부분 이름 인식 : 전체 이름이나 별칭을 철자 할 필요가 없습니다. 파서가 속성 / 별칭을 다른 것과 명확하게 구분할 수 있도록 철자 만 입력하면됩니다.
- ClickOnce 지원 : ClickOnce 배포 응용 프로그램의 URL에 쿼리 문자열로 지정된 경우에도 속성을 초기화 할 수 있습니다. 명령 줄 초기화 메서드는 ClickOnce로 실행 중인지 여부를 감지하므로 사용할 때 코드를 변경할 필요가 없습니다.
- 자동 생성 /? help : 여기에는 콘솔의 너비를 고려한 멋진 형식이 포함됩니다.
- 파일에 명령 줄 인수로드 / 저장 : 여러 번 실행하려는 크고 복잡한 명령 줄 인수 집합이 여러 개있는 경우 특히 유용합니다.
잠시 전에 C # 명령 줄 인수 파서를 작성했습니다. 그 위치 : http://www.codeplex.com/CommandLineArguments
CLAP (명령 줄 인수 파서)에는 사용 가능한 API가 있으며 훌륭하게 문서화되어 있습니다. 매개 변수에 주석을 달아 메서드를 만듭니다. https://github.com/adrianaisemberg/CLAP
이 문제에 대한 수많은 해결책이 있습니다. 완전성을 위해 누군가가 원하는 경우 대안을 제공하기 위해 Google 코드 라이브러리의 두 가지 유용한 클래스에 대해이 답변을 추가하고 있습니다 .
첫 번째는 명령 줄 매개 변수 구문 분석만을 담당하는 ArgumentList입니다. '/ x : y'또는 '-x = y'스위치로 정의 된 이름-값 쌍을 수집하고 '이름이 지정되지 않은'항목 목록도 수집합니다. 기본적인 사용법은 여기 에서 설명 합니다 . 여기에서 클래스를 확인하세요 .
두 번째 부분은 .Net 클래스에서 완전한 기능의 명령 줄 응용 프로그램을 만드는 CommandInterpreter 입니다. 예로서:
using CSharpTest.Net.Commands;
static class Program
{
static void Main(string[] args)
{
new CommandInterpreter(new Commands()).Run(args);
}
//example ‘Commands’ class:
class Commands
{
public int SomeValue { get; set; }
public void DoSomething(string svalue, int ivalue)
{ ... }
위의 예제 코드를 사용하여 다음을 실행할 수 있습니다.
Program.exe DoSomething "문자열 값"5
-또는-
Program.exe 작업 / ivalue = 5 -svalue : "문자열 값"
필요한만큼 간단하거나 복잡합니다. 당신은 수있는 소스 코드를 검토 , 도움을 보거나 , 또는 바이너리를 다운로드 .
나는 당신이 인수에 대한 "규칙을 정의"할 수 있기 때문에 그것을 좋아 합니다 .
또는 Unix 사용자라면 GNU Getopt .NET 포트를 좋아할 것 입니다.
사용하기 쉽고 확장 가능한 명령 줄 인수 파서. 핸들 : 부울, 플러스 / 마이너스, 문자열, 문자열 목록, CSV, 열거.
'/?'에 내장 도움말 모드.
'/ ??'에 내장 및 '/? D'문서 생성기 모드.
static void Main(string[] args)
{
// create the argument parser
ArgumentParser parser = new ArgumentParser("ArgumentExample", "Example of argument parsing");
// create the argument for a string
StringArgument StringArg = new StringArgument("String", "Example string argument", "This argument demonstrates string arguments");
// add the argument to the parser
parser.Add("/", "String", StringArg);
// parse arguemnts
parser.Parse(args);
// did the parser detect a /? argument
if (parser.HelpMode == false)
{
// was the string argument defined
if (StringArg.Defined == true)
{
// write its value
RC.WriteLine("String argument was defined");
RC.WriteLine(StringArg.Value);
}
}
}
편집 : 이것은 내 프로젝트 이므로이 답변은 타사의 보증으로 간주되어서는 안됩니다. 그것은 내가 작성하는 모든 명령 줄 기반 프로그램에 사용한다고 말했고, 오픈 소스이며 다른 사람들이 이로부터 혜택을받을 수 있기를 바랍니다.
http://www.codeplex.com/commonlibrarynet에 명령 줄 인수 파서가 있습니다 .
1. 속성을 사용하여 인수를 구문 분석 할 수 있습니다 .
2. 명시 적 호출
3. 여러 인수의 한 줄 또는 문자열 배열
다음과 같은 것을 처리 할 수 있습니다.
- 설정 : 품질 보증 - STARTDATE : $ { 오늘 } - 지역 : '뉴욕'Settings01
사용하기 매우 쉽습니다.
이것은 Novell Options
클래스를 기반으로 작성한 핸들러 입니다.
이것은 while (input !="exit")
예를 들어 FTP 콘솔과 같은 대화 형 콘솔 인 스타일 루프 를 실행하는 콘솔 응용 프로그램을 대상 으로합니다.
사용 예 :
static void Main(string[] args)
{
// Setup
CommandHandler handler = new CommandHandler();
CommandOptions options = new CommandOptions();
// Add some commands. Use the v syntax for passing arguments
options.Add("show", handler.Show)
.Add("connect", v => handler.Connect(v))
.Add("dir", handler.Dir);
// Read lines
System.Console.Write(">");
string input = System.Console.ReadLine();
while (input != "quit" && input != "exit")
{
if (input == "cls" || input == "clear")
{
System.Console.Clear();
}
else
{
if (!string.IsNullOrEmpty(input))
{
if (options.Parse(input))
{
System.Console.WriteLine(handler.OutputMessage);
}
else
{
System.Console.WriteLine("I didn't understand that command");
}
}
}
System.Console.Write(">");
input = System.Console.ReadLine();
}
}
그리고 출처 :
/// <summary>
/// A class for parsing commands inside a tool. Based on Novell Options class (http://www.ndesk.org/Options).
/// </summary>
public class CommandOptions
{
private Dictionary<string, Action<string[]>> _actions;
private Dictionary<string, Action> _actionsNoParams;
/// <summary>
/// Initializes a new instance of the <see cref="CommandOptions"/> class.
/// </summary>
public CommandOptions()
{
_actions = new Dictionary<string, Action<string[]>>();
_actionsNoParams = new Dictionary<string, Action>();
}
/// <summary>
/// Adds a command option and an action to perform when the command is found.
/// </summary>
/// <param name="name">The name of the command.</param>
/// <param name="action">An action delegate</param>
/// <returns>The current CommandOptions instance.</returns>
public CommandOptions Add(string name, Action action)
{
_actionsNoParams.Add(name, action);
return this;
}
/// <summary>
/// Adds a command option and an action (with parameter) to perform when the command is found.
/// </summary>
/// <param name="name">The name of the command.</param>
/// <param name="action">An action delegate that has one parameter - string[] args.</param>
/// <returns>The current CommandOptions instance.</returns>
public CommandOptions Add(string name, Action<string[]> action)
{
_actions.Add(name, action);
return this;
}
/// <summary>
/// Parses the text command and calls any actions associated with the command.
/// </summary>
/// <param name="command">The text command, e.g "show databases"</param>
public bool Parse(string command)
{
if (command.IndexOf(" ") == -1)
{
// No params
foreach (string key in _actionsNoParams.Keys)
{
if (command == key)
{
_actionsNoParams[key].Invoke();
return true;
}
}
}
else
{
// Params
foreach (string key in _actions.Keys)
{
if (command.StartsWith(key) && command.Length > key.Length)
{
string options = command.Substring(key.Length);
options = options.Trim();
string[] parts = options.Split(' ');
_actions[key].Invoke(parts);
return true;
}
}
}
return false;
}
}
개인적으로 좋아하는 것은 Peter Palotas의 http://www.codeproject.com/KB/recipes/plossum_commandline.aspx입니다 .
[CommandLineManager(ApplicationName="Hello World",
Copyright="Copyright (c) Peter Palotas")]
class Options
{
[CommandLineOption(Description="Displays this help text")]
public bool Help = false;
[CommandLineOption(Description = "Specifies the input file", MinOccurs=1)]
public string Name
{
get { return mName; }
set
{
if (String.IsNullOrEmpty(value))
throw new InvalidOptionValueException(
"The name must not be empty", false);
mName = value;
}
}
private string mName;
}
최근에 FubuCore 명령 줄 구문 분석 구현을 발견했습니다. 그 이유는 다음과 같습니다.
- 사용하기 쉽습니다. 문서를 찾을 수는 없지만 FubuCore 솔루션은 어떤 문서보다 기능에 대해 더 많이 말하는 멋진 단위 테스트 세트가 포함 된 프로젝트를 제공합니다.
- 그것은 멋진 객체 지향 디자인, 코드 반복 또는 명령 줄 구문 분석 앱에서 사용했던 다른 것들이 없습니다.
- 선언적입니다 : 기본적으로 명령 및 매개 변수 세트에 대한 클래스를 작성하고 속성으로 장식하여 다양한 옵션 (예 : 이름, 설명, 필수 / 선택 사항)을 설정합니다.
- 라이브러리는 이러한 정의를 기반으로 멋진 사용 그래프를 인쇄합니다.
아래는 이것을 사용하는 방법에 대한 간단한 예입니다. 사용법을 설명하기 위해 다음과 같은 두 가지 명령이있는 간단한 유틸리티를 작성했습니다. 현재 추가 된 모든 개체)
먼저 'add'명령에 대한 Command 클래스를 작성했습니다.
[Usage("add", "Adds an object to the list")]
[CommandDescription("Add object", Name = "add")]
public class AddCommand : FubuCommand<CommandInput>
{
public override bool Execute(CommandInput input)
{
State.Objects.Add(input); // add the new object to an in-memory collection
return true;
}
}
이 명령은 CommandInput 인스턴스를 매개 변수로 사용하므로 다음에 정의합니다.
public class CommandInput
{
[RequiredUsage("add"), Description("The name of the object to add")]
public string ObjectName { get; set; }
[ValidUsage("add")]
[Description("The value of the object to add")]
public int ObjectValue { get; set; }
[Description("Multiply the value by -1")]
[ValidUsage("add")]
[FlagAlias("nv")]
public bool NegateValueFlag { get; set; }
}
다음 명령은 'list'이며 다음과 같이 구현됩니다.
[Usage("list", "List the objects we have so far")]
[CommandDescription("List objects", Name = "list")]
public class ListCommand : FubuCommand<NullInput>
{
public override bool Execute(NullInput input)
{
State.Objects.ForEach(Console.WriteLine);
return false;
}
}
'list'명령은 매개 변수를 사용하지 않으므로이를 위해 NullInput 클래스를 정의했습니다.
public class NullInput { }
이제 남은 것은 다음과 같이 Main () 메서드에 연결하는 것입니다.
static void Main(string[] args)
{
var factory = new CommandFactory();
factory.RegisterCommands(typeof(Program).Assembly);
var executor = new CommandExecutor(factory);
executor.Execute(args);
}
프로그램은 예상대로 작동하며 명령이 유효하지 않은 경우 올바른 사용법에 대한 힌트를 인쇄합니다.
------------------------
Available commands:
------------------------
add -> Add object
list -> List objects
------------------------
그리고 'add'명령의 샘플 사용법 :
Usages for 'add' (Add object)
add <objectname> [-nv]
-------------------------------------------------
Arguments
-------------------------------------------------
objectname -> The name of the object to add
objectvalue -> The value of the object to add
-------------------------------------------------
-------------------------------------
Flags
-------------------------------------
[-nv] -> Multiply the value by -1
-------------------------------------
Powershell 커맨드 렛.
커맨드 렛에 지정된 속성, 유효성 검사 지원, 매개 변수 세트, 파이프 라이닝, 오류보고, 도움말 및 다른 커맨드 렛에서 사용하기 위해 반환되는 모든 .NET 개체에 따라 powershell에서 구문 분석을 수행합니다.
시작하는 데 도움이되는 몇 가지 링크 :
C # CLI 는 제가 작성한 매우 간단한 명령 줄 인수 구문 분석 라이브러리입니다. 잘 문서화되고 오픈 소스입니다.
Genghis Command Line Parser 는 다소 구식 일 수 있지만 기능이 매우 완벽하고 저에게 잘 작동합니다.
오픈 소스 라이브러리 CSharpOptParse를 제안합니다 . 명령 줄을 구문 분석하고 명령 줄 입력을 사용하여 사용자 정의 .NET 개체를 수화합니다. 저는 C # 콘솔 애플리케이션을 작성할 때 항상이 라이브러리를 사용합니다.
Apache Commons CLI API의 .net 포트를 사용하십시오. 이것은 훌륭하게 작동합니다.
http://sourceforge.net/projects/dotnetcli/
개념 및 소개를위한 원래 API
http://commons.apache.org/cli/
기본 인수를 지원하는 명령 줄 구문 분석을위한 매우 간단하고 사용하기 쉬운 임시 클래스입니다.
class CommandLineArgs
{
public static CommandLineArgs I
{
get
{
return m_instance;
}
}
public string argAsString( string argName )
{
if (m_args.ContainsKey(argName)) {
return m_args[argName];
}
else return "";
}
public long argAsLong(string argName)
{
if (m_args.ContainsKey(argName))
{
return Convert.ToInt64(m_args[argName]);
}
else return 0;
}
public double argAsDouble(string argName)
{
if (m_args.ContainsKey(argName))
{
return Convert.ToDouble(m_args[argName]);
}
else return 0;
}
public void parseArgs(string[] args, string defaultArgs )
{
m_args = new Dictionary<string, string>();
parseDefaults(defaultArgs );
foreach (string arg in args)
{
string[] words = arg.Split('=');
m_args[words[0]] = words[1];
}
}
private void parseDefaults(string defaultArgs )
{
if ( defaultArgs == "" ) return;
string[] args = defaultArgs.Split(';');
foreach (string arg in args)
{
string[] words = arg.Split('=');
m_args[words[0]] = words[1];
}
}
private Dictionary<string, string> m_args = null;
static readonly CommandLineArgs m_instance = new CommandLineArgs();
}
class Program
{
static void Main(string[] args)
{
CommandLineArgs.I.parseArgs(args, "myStringArg=defaultVal;someLong=12");
Console.WriteLine("Arg myStringArg : '{0}' ", CommandLineArgs.I.argAsString("myStringArg"));
Console.WriteLine("Arg someLong : '{0}' ", CommandLineArgs.I.argAsLong("someLong"));
}
}
참고 URL : https://stackoverflow.com/questions/491595/best-way-to-parse-command-line-arguments-in-c
'Nice programing' 카테고리의 다른 글
Objective-C에서 난수 생성 (0) | 2020.09.29 |
---|---|
차이 (0) | 2020.09.29 |
String.slice와 String.substring의 차이점은 무엇입니까? (0) | 2020.09.29 |
Objective-C에서 델리게이트를 어떻게 생성합니까? (0) | 2020.09.29 |
pg gem을 설치하려고 할 때 'libpq-fe.h 헤더를 찾을 수 없습니다. (0) | 2020.09.29 |