[PowerToys Run] Add Suport for Commandline arguments in Program Plugin (#5791)

* Implemented possibility to add commandline arguments in the Program Plugin

* Add missing return statement inc ommandArgumentParser loop

* Fix typos

* Fix Additional Typo

* Changed -c to /c to make it a valid cmd argument

* Added small comment about importance of order in _programArgumentParsers

Co-authored-by: Roy <royvou@hotmailcom>
This commit is contained in:
Roy
2020-09-18 00:39:28 +02:00
committed by GitHub
parent 923972dd49
commit b3833fcf1a
10 changed files with 223 additions and 23 deletions

View File

@@ -0,0 +1,43 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Linq;
using Wox.Plugin;
namespace Microsoft.Plugin.Program
{
public class DoubleDashProgramArgumentParser : IProgramArgumentParser
{
private const string DoubleDash = "--";
public bool Enabled { get; } = true;
public bool TryParse(Query query, out string program, out string programArguments)
{
if (!string.IsNullOrEmpty(query?.Search))
{
// First Argument is always (part of) the program, 2nd term is possibly a Program Argument
if (query.Terms.Length > 1)
{
for (var i = 1; i < query.Terms.Length; i++)
{
if (!string.Equals(query.Terms[i], DoubleDash, StringComparison.Ordinal))
{
continue;
}
program = string.Join(Query.TermSeparator, query.Terms.Take(i));
programArguments = string.Join(Query.TermSeparator, query.Terms.Skip(i + 1));
return true;
}
}
}
program = null;
programArguments = null;
return false;
}
}
}

View File

@@ -0,0 +1,43 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Linq;
using System.Text.RegularExpressions;
using Wox.Plugin;
namespace Microsoft.Plugin.Program
{
public class InferedProgramArgumentParser : IProgramArgumentParser
{
private static readonly Regex ArgumentPrefixRegex = new Regex("^(-|--|/)[a-zA-Z]+", RegexOptions.Compiled);
public bool Enabled { get; } = true;
public bool TryParse(Query query, out string program, out string programArguments)
{
if (!string.IsNullOrEmpty(query?.Search))
{
// First Argument is always (part of) the program, 2nd term is possibly a Program Argument
if (query.Terms.Length > 1)
{
for (var i = 1; i < query.Terms.Length; i++)
{
if (!ArgumentPrefixRegex.IsMatch(query.Terms[i]))
{
continue;
}
program = string.Join(Query.TermSeparator, query.Terms.Take(i));
programArguments = string.Join(Query.TermSeparator, query.Terms.Skip(i));
return true;
}
}
}
program = null;
programArguments = null;
return false;
}
}
}

View File

@@ -0,0 +1,20 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Wox.Plugin;
namespace Microsoft.Plugin.Program.ProgramArgumentParser
{
public class NoArgumentsArgumentParser : IProgramArgumentParser
{
public bool Enabled { get; } = true;
public bool TryParse(Query query, out string program, out string programArguments)
{
program = query?.Search;
programArguments = null;
return true;
}
}
}