-
Notifications
You must be signed in to change notification settings - Fork 6.6k
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
Aug 14, 2020
1 parent
5c1713f
commit 02b3c8b
Showing
12 changed files
with
654 additions
and
485 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
...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,53 @@ | ||
// 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 | ||
{ | ||
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", null)] | ||
[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
11 changes: 11 additions & 0 deletions
11
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,11 @@ | ||
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
37 changes: 37 additions & 0 deletions
37
...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,37 @@ | ||
using System; | ||
using System.Linq; | ||
using Wox.Plugin; | ||
|
||
namespace Microsoft.Plugin.Program | ||
{ | ||
public class DoubleDashProgramArgumentParser : IProgramArgumentParser | ||
{ | ||
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 (int i = 1; i < query.Terms.Length; i++) | ||
{ | ||
if (string.Equals(query.Terms[i], doubleDash, StringComparison.Ordinal)) | ||
{ | ||
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; | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...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,38 @@ | ||
using System.Collections.Generic; | ||
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 (int i = 1; i < query.Terms.Length; i++) | ||
{ | ||
if (ArgumentPrefixRegex.IsMatch(query.Terms[i])) | ||
{ | ||
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; | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...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,16 @@ | ||
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
Oops, something went wrong.