-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented possibility to add commandline arguments in the Program P…
…lugin
- Loading branch information
Roy
committed
Sep 10, 2020
1 parent
ab8bec8
commit 2050815
Showing
11 changed files
with
238 additions
and
42 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
...ns/Microsoft.Plugin.Program.UnitTests/ProgramArgumentParser/ProgramArgumentParserTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// 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 Microsoft.Plugin.Program.ProgramArgumentParser; | ||
using NUnit.Framework; | ||
using Wox.Plugin; | ||
|
||
namespace Microsoft.Plugin.Program.UnitTests.ProgramArgumentParser | ||
{ | ||
[TestFixture] | ||
public class ProgramArgumentParserTests | ||
{ | ||
[TestCase("Microsoft Edge", "Microsoft Edge", null)] | ||
[TestCase("Microsoft Edge ---inprivate", "Microsoft Edge ---inprivate", null)] | ||
[TestCase("Microsoft Edge -- -inprivate", "Microsoft Edge", "-inprivate")] | ||
[TestCase("Microsoft Edge -inprivate", "Microsoft Edge", "-inprivate")] | ||
[TestCase("Microsoft Edge /inprivate", "Microsoft Edge", "/inprivate")] | ||
[TestCase("edge.exe --inprivate", "edge.exe", "--inprivate")] | ||
[TestCase("edge.exe -- --inprivate", "edge.exe", "--inprivate")] | ||
[TestCase("edge.exe", "edge.exe", null)] | ||
[TestCase("edge", "edge", null)] | ||
[TestCase("cmd -c \"ping 1.1.1.1\"", "cmd", "-c \"ping 1.1.1.1\"")] | ||
public void ProgramArgumentParserTestsCanParseQuery(string inputQuery, string expectedProgram, string expectedProgramArguments) | ||
{ | ||
// Arrange | ||
var argumentParsers = new IProgramArgumentParser[] | ||
{ | ||
new DoubleDashProgramArgumentParser(), | ||
new InferedProgramArgumentParser(), | ||
new NoArgumentsArgumentParser(), | ||
}; | ||
|
||
// Basis version oft he Quey parser which can be found at Wox.Core.Plugin.QueryBuilder but didn't want to conenct this to that project | ||
var splittedSearchString = inputQuery?.Split(Query.TermSeparator, System.StringSplitOptions.RemoveEmptyEntries); | ||
var cleanQuery = string.Join(Query.TermSeparator, splittedSearchString); | ||
var query = new Query(cleanQuery, cleanQuery, splittedSearchString, string.Empty); | ||
|
||
// Act | ||
string program = null, programArguments = null; | ||
foreach (var argumentParser in argumentParsers) | ||
{ | ||
if (argumentParser.TryParse(query, out program, out programArguments)) | ||
{ | ||
break; | ||
} | ||
} | ||
|
||
// Assert | ||
Assert.AreEqual(expectedProgram, program); | ||
Assert.AreEqual(expectedProgramArguments, programArguments); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/modules/launcher/Plugins/Microsoft.Plugin.Program/Interface/IProgramArgumentParser.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// 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 | ||
{ | ||
public interface IProgramArgumentParser | ||
{ | ||
bool Enabled { get; } | ||
|
||
bool TryParse(Query query, out string program, out string programArguments); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...Plugins/Microsoft.Plugin.Program/ProgramArgumentParser/DoubleDashProgramArgumentParser.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 programa, 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; | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...er/Plugins/Microsoft.Plugin.Program/ProgramArgumentParser/InferedProgramArgumentParser.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 programa, 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; | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...ncher/Plugins/Microsoft.Plugin.Program/ProgramArgumentParser/NoArgumentsArgumentParser.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters