Skip to content

Suggestion: Argument file support #36

@LightTempler

Description

@LightTempler

For use cases like Windows Planned Tasks or longer, auto generated commandlines using a textfile instead of commandline has advantages. I just checked successfully, a single line of code is enough to get full argument file support for CommandLineParser :

MyApp.exe "C:\Configs\MyCmdlineFile.txt"

VB.NET (checked)

Sub Main(args() As String)

Dim MyOptionsClass As new cMyOptionsClass
Dim clParser As New CommandLineParser.CommandLineParser
Try
     clParser.ExtractArgumentAttributes(MyOptionsClass)

    ' JUST ADD THIS LINE:
    If args IsNot Nothing AndAlso args.Count = 1 AndAlso File.Exists(args(0)) = True Then args= File.ReadAllText(args(0)).Trim.Split(New String(){" ", vbCrLf}, StringSplitOptions.RemoveEmptyEntries)

    clParser.ParseCommandLine(args)

    ' Here we go using values ...

    Catch clEx As CommandLineException
            Console.WriteLine(clEx.Message)

    Catch ex As Exception
            Console.WriteLine(Ex.Message)
End Try
End Sub

C# (unchecked!, sorry)

static class MyApp
{	public static void Main(string[] args)
	{
		cMyOptionsClass MyOptionsClass = new cMyOptionsClass();
		CommandLineParser.CommandLineParser clParser = new CommandLineParser.CommandLineParser();
		try {
			clParser.ExtractArgumentAttributes(MyOptionsClass);

			// JUST ADD THIS LINE:
			if (args != null && args.Count == 1 && File.Exists(args(0)) == true)
				args = File.ReadAllText(args(0)).Trim.Split(new string[] {
					" ",
					Strings.Chr(13) + Strings.Chr(10)
				}, StringSplitOptions.RemoveEmptyEntries);

			clParser.ParseCommandLine(args);

			// Here we go using values ...

		} catch (CommandLineException clEx) {
			Console.WriteLine(clEx.Message);

		} catch (Exception ex) {
			Console.WriteLine(ex.Message);
		}
	}
}

Maybe this could be a switchable, build-in feature in a further version or it is worth to add to docu.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions