Skip to content
This repository was archived by the owner on Jan 24, 2025. It is now read-only.
This repository was archived by the owner on Jan 24, 2025. It is now read-only.

Use a command line parsing library #481

@jzabroski

Description

@jzabroski

static void EvaluateArguments(string[] args)
{
// BucketName=nantokakantokabucket
BucketName = args.Where(x => x.StartsWith(ArgumentType.BucketName.ToString(), StringComparison.InvariantCultureIgnoreCase))
.SelectMany(x => x.SplitEx("="))
.LastOrDefault()
?? GetEnvValueString(ArgumentType.BucketName, EnvType.S3Sync_BucketName);
// LocalRoot=c:\hogemoge
LocalRoot = args.Where(x => x.StartsWith(ArgumentType.LocalRoot.ToString(), StringComparison.InvariantCultureIgnoreCase))
.SelectMany(x => x.SplitEx("="))
.LastOrDefault()
?? GetEnvValueString(ArgumentType.LocalRoot, EnvType.S3Sync_LocalRoot);
// KeyPrefix=image
KeyPrefix = args.Where(x => x.StartsWith(ArgumentType.KeyPrefix.ToString(), StringComparison.InvariantCultureIgnoreCase))
.SelectMany(x => x.SplitEx("="))
.LastOrDefault()
?.TrimEnd('/')
?? GetEnvValueString(ArgumentType.KeyPrefix, EnvType.S3Sync_KeyPrefix);
// IgnoreKeyPrefix=image
IgnoreKeyPrefix = args.Where(x => x.StartsWith(ArgumentType.IgnoreKeyPrefix.ToString(), StringComparison.InvariantCultureIgnoreCase))
.SelectMany(x => x.SplitEx("="))
.LastOrDefault()
?.TrimEnd('/')
?? GetEnvValueString(ArgumentType.IgnoreKeyPrefix, EnvType.S3Sync_IgnoreKeyPrefix);
// ExcludeFiles=hogemoge,fugafuga
ExcludeFiles = args.Where(x => x.StartsWith(ArgumentType.ExcludeFiles.ToString(), StringComparison.InvariantCultureIgnoreCase))
.SelectMany(x => x.SplitEx("="))
.LastOrDefault()
?.SplitEx(",")
.Select(x => x.Trim())
.ToArray()
?? GetEnvValueString(ArgumentType.ExcludeFiles, EnvType.S3Sync_ExcludeFiles)
?.SplitEx(",");
// ExcludeDirectories=hogemoge,fugafuga
ExcludeDirectories = args.Where(x => x.StartsWith(ArgumentType.ExcludeDirectories.ToString(), StringComparison.InvariantCultureIgnoreCase))
.SelectMany(x => x.SplitEx("="))
.LastOrDefault()
?.SplitEx(",")
?.Select(x => x.Trim())
.ToArray()
?? GetEnvValueString(ArgumentType.ExcludeDirectories, EnvType.S3Sync_ExcludeDirectories)
?.SplitEx(",");
// Silent=false
Silent = bool.Parse(args.Where(x => x.StartsWith(ArgumentType.Silent.ToString(), StringComparison.InvariantCultureIgnoreCase))
.SelectMany(x => x.SplitEx("="))
.Where(x => string.Equals(x, "true", StringComparison.InvariantCultureIgnoreCase) || string.Equals(x, "false", StringComparison.InvariantCultureIgnoreCase))
.LastOrDefault()
?.Trim()
?? GetEnvValueString(ArgumentType.Silent, EnvType.S3Sync_Silent)
?? "false");
// CredentialProfile=ProfileName
CredentialProfile = args.Where(x => x.StartsWith(ArgumentType.CredentialProfile.ToString(), StringComparison.InvariantCultureIgnoreCase))
.SelectMany(x => x.SplitEx("="))
.LastOrDefault()
?.Trim()
?? GetEnvValueString(ArgumentType.CredentialProfile, EnvType.S3Sync_CredentialProfile);
// DryRun=true
Option.DryRun = bool.Parse(args.Where(x => x.StartsWith(ArgumentType.DryRun.ToString(), StringComparison.InvariantCultureIgnoreCase))
.SelectMany(x => x.SplitEx("="))
.Where(x => string.Equals(x, "true", StringComparison.InvariantCultureIgnoreCase) || string.Equals(x, "false", StringComparison.InvariantCultureIgnoreCase))
.LastOrDefault()
?.Trim()
?? GetEnvValueString(ArgumentType.DryRun, EnvType.S3Sync_DryRun)
?? "true");
// ContentType=application/json
Option.ContentType = args.Where(x => x.StartsWith(ArgumentType.ContentType.ToString(), StringComparison.InvariantCultureIgnoreCase))
.SelectMany(x => x.SplitEx("="))
.LastOrDefault()
?.TrimEnd('/')
?? GetEnvValueString(ArgumentType.ContentType, EnvType.S3Sync_ContentType);
// Region=us-east-1
Option.Region = args.Where(x => x.StartsWith(ArgumentType.Region.ToString(), StringComparison.InvariantCultureIgnoreCase))
.SelectMany(x => x.SplitEx("="))
.LastOrDefault()
?.TrimEnd('/')
?? GetEnvValueString(ArgumentType.Region, EnvType.S3Sync_Region);
// Show Arguments
Log($"{nameof(BucketName)} : {BucketName}");
Log($"{nameof(LocalRoot)} : {LocalRoot}");
Log($"{nameof(KeyPrefix)} : {KeyPrefix}");
Log($"{nameof(IgnoreKeyPrefix)} : {IgnoreKeyPrefix}");
Log($"{nameof(ExcludeFiles)} : {ExcludeFiles?.ToJoinedString(",")}");
Log($"{nameof(ExcludeDirectories)} : {ExcludeDirectories?.ToJoinedString(",")}");
Log($"{nameof(Silent)} : {Silent}");
Log($"{nameof(CredentialProfile)} : {CredentialProfile}");
Log($"{nameof(Option.DryRun)} : {Option.DryRun}");
Log($"{nameof(Option.ContentType)} : {Option.ContentType}");
Log($"{nameof(Option.Region)} : {Option.Region}");
// Validate Required arguments
if (string.IsNullOrWhiteSpace(BucketName))
{
Error("Please pass arguments. See detail followings.");
PrintHelp();
throw new NullReferenceException(nameof(BucketName));
}
if (string.IsNullOrWhiteSpace(LocalRoot))
{
Error("Please pass arguments. See detail followings.");
PrintHelp();
throw new NullReferenceException(nameof(LocalRoot));
}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions