Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a command line parsing library #481

Open
jzabroski opened this issue Jul 28, 2021 · 0 comments
Open

Use a command line parsing library #481

jzabroski opened this issue Jul 28, 2021 · 0 comments

Comments

@jzabroski
Copy link

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));
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant