What is best/recommended way to handle "cross platform" paths?
Opened this issue · 0 comments
TraGicCode commented
I have a command line application where i want to allow almost every command to accept a --config option. This will allow the user to specify a custom location in which the cli configuration file is located on their file system. By default, if this argument isn't passed, i would like it to default to the to a folder and file in the users home directory. On windows, [DefaultValue("~/.my-cli-app/config.yaml")] works like a charm. On linux, "~/.my-cli-app/config.yaml" is not a valid path.
I was able to get this to work using Validate() as shown below. This though feels wrong. Is there a cleaner better way to do this?
public class GlobalCommandSettings : CommandSettings
{
// [CommandOption("--verbose")]
// public bool Verbose { get; set; }
[CommandOption("--config <FILE>")]
[Description("Path to the config.yaml file to use for the CLI.")]
[DefaultValue("~/.my-cli-app/config.yaml")]
public string Config { get; set; }
public override ValidationResult Validate()
{
if (Config == "~/.my-cli-app/config.yaml")
{
// Transform the default home path so it will work on linux appropriately
Config = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
".my-cli-app",
"config.yaml");
Console.WriteLine(Config);
}
return ValidationResult.Success();
}
}