Skip to content

Commit

Permalink
Manually brought over changes from ArgumentParser-migration branch
Browse files Browse the repository at this point in the history
Resolves #17 and #1
  • Loading branch information
macmule committed May 26, 2022
1 parent ef60723 commit 1c235c2
Show file tree
Hide file tree
Showing 10 changed files with 345 additions and 233 deletions.
Binary file modified Notifier/.DS_Store
Binary file not shown.
36 changes: 20 additions & 16 deletions Notifier/Notifier.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
7C3ECE5023E057FE008E3D9D /* SPMUtility in Frameworks */ = {isa = PBXBuildFile; productRef = 7C3ECE4F23E057FE008E3D9D /* SPMUtility */; };
63B92DE1282AAEB700B44340 /* ArgumentParser in Frameworks */ = {isa = PBXBuildFile; productRef = 63B92DE0282AAEB700B44340 /* ArgumentParser */; };
63B92DE9282AAEDE00B44340 /* ArgParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63B92DE8282AAEDE00B44340 /* ArgParser.swift */; };
7C6D157C2270BA2F0092D34E /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C6D157B2270BA2F0092D34E /* AppDelegate.swift */; };
7C6D15812270BA300092D34E /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 7C6D157F2270BA300092D34E /* MainMenu.xib */; };
7C720952227888E10033B2E2 /* Functions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C720951227888E10033B2E2 /* Functions.swift */; };
Expand Down Expand Up @@ -71,6 +72,7 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
63B92DE8282AAEDE00B44340 /* ArgParser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArgParser.swift; sourceTree = "<group>"; };
7C6D15782270BA2F0092D34E /* Notifier.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Notifier.app; sourceTree = BUILT_PRODUCTS_DIR; };
7C6D157B2270BA2F0092D34E /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
7C6D15802270BA300092D34E /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/MainMenu.xib; sourceTree = "<group>"; };
Expand All @@ -89,7 +91,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
7C3ECE5023E057FE008E3D9D /* SPMUtility in Frameworks */,
63B92DE1282AAEB700B44340 /* ArgumentParser in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -123,6 +125,7 @@
7C720951227888E10033B2E2 /* Functions.swift */,
7C6D15822270BA300092D34E /* Info.plist */,
7C6D157F2270BA300092D34E /* MainMenu.xib */,
63B92DE8282AAEDE00B44340 /* ArgParser.swift */,
);
path = Notifier;
sourceTree = "<group>";
Expand Down Expand Up @@ -184,7 +187,7 @@
);
name = Notifier;
packageProductDependencies = (
7C3ECE4F23E057FE008E3D9D /* SPMUtility */,
63B92DE0282AAEB700B44340 /* ArgumentParser */,
);
productName = oioi;
productReference = 7C6D15782270BA2F0092D34E /* Notifier.app */;
Expand Down Expand Up @@ -224,7 +227,7 @@
);
mainGroup = 7C6D156F2270BA2F0092D34E;
packageReferences = (
7C3ECE4E23E057FE008E3D9D /* XCRemoteSwiftPackageReference "swift-package-manager" */,
63B92DDF282AAEB700B44340 /* XCRemoteSwiftPackageReference "swift-argument-parser" */,
);
productRefGroup = 7C6D15792270BA2F0092D34E /* Products */;
projectDirPath = "";
Expand Down Expand Up @@ -301,6 +304,7 @@
files = (
7C6D157C2270BA2F0092D34E /* AppDelegate.swift in Sources */,
7C720952227888E10033B2E2 /* Functions.swift in Sources */,
63B92DE9282AAEDE00B44340 /* ArgParser.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -383,7 +387,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.9;
MACOSX_DEPLOYMENT_TARGET = 10.10;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
ONLY_ACTIVE_ARCH = YES;
Expand Down Expand Up @@ -439,7 +443,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.9;
MACOSX_DEPLOYMENT_TARGET = 10.10;
MTL_ENABLE_DEBUG_INFO = NO;
MTL_FAST_MATH = YES;
SDKROOT = macosx;
Expand All @@ -453,17 +457,17 @@
buildSettings = {
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_IDENTITY = "-";
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
DEVELOPMENT_TEAM = "";
DEVELOPMENT_TEAM = 82K2XFN8L6;
ENABLE_HARDENED_RUNTIME = YES;
INFOPLIST_FILE = Notifier/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.9;
MACOSX_DEPLOYMENT_TARGET = 10.10;
MARKETING_VERSION = 2.2.3;
PRODUCT_BUNDLE_IDENTIFIER = uk.co.dataJAR.Notifier;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand All @@ -488,7 +492,7 @@
"$(inherited)",
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.9;
MACOSX_DEPLOYMENT_TARGET = 10.10;
MARKETING_VERSION = 2.2.3;
PRODUCT_BUNDLE_IDENTIFIER = uk.co.dataJAR.Notifier;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand Down Expand Up @@ -522,21 +526,21 @@
/* End XCConfigurationList section */

/* Begin XCRemoteSwiftPackageReference section */
7C3ECE4E23E057FE008E3D9D /* XCRemoteSwiftPackageReference "swift-package-manager" */ = {
63B92DDF282AAEB700B44340 /* XCRemoteSwiftPackageReference "swift-argument-parser" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/apple/swift-package-manager.git";
repositoryURL = "https://github.com/apple/swift-argument-parser.git";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 0.5.0;
minimumVersion = 1.0.0;
};
};
/* End XCRemoteSwiftPackageReference section */

/* Begin XCSwiftPackageProductDependency section */
7C3ECE4F23E057FE008E3D9D /* SPMUtility */ = {
63B92DE0282AAEB700B44340 /* ArgumentParser */ = {
isa = XCSwiftPackageProductDependency;
package = 7C3ECE4E23E057FE008E3D9D /* XCRemoteSwiftPackageReference "swift-package-manager" */;
productName = SPMUtility;
package = 63B92DDF282AAEB700B44340 /* XCRemoteSwiftPackageReference "swift-argument-parser" */;
productName = ArgumentParser;
};
/* End XCSwiftPackageProductDependency section */
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,4 @@
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
</Scheme>
126 changes: 75 additions & 51 deletions Notifier/Notifier/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import Cocoa
import CoreFoundation
import SPMUtility


@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
Expand All @@ -32,18 +32,6 @@ class AppDelegate: NSObject, NSApplicationDelegate {
// Define args to be parsed
do {

let argParser = ArgumentParser(commandName: "notifier", usage: "--type <alert/banner> --message <some message> <options>", overview: "Notifier: Sends banner or alert notifications.", seeAlso: "https://github.com/dataJAR/Notifier")
let ncMessage = argParser.add(option: "--message", kind: String.self, usage: "message text - REQUIRED if not passing --remove all")
let ncMessageAction = argParser.add(option: "--messageaction", kind: String.self, usage: "The action to be performed when the message is clicked. Either pass 'logout' or path to item to open on click. Can be a .app, file, URL etc. With non-.app items being opened in their default handler")
let ncMessageButton = argParser.add(option: "--messagebutton", kind: String.self, usage: "alert type only. Sets the message buttons text")
let ncMessageButtonAction = argParser.add(option: "--messagebuttonaction", kind: String.self, usage: "alert type only. The action to be performed when the message button is clicked. Either pass 'logout' or path to item to open on click. Can be a .app, file, URL etc. With non-.app items being opened in their default handler. Requires '--messagebutton' to be passed")
let ncRemove = argParser.add(option: "--remove", kind: String.self, usage: "\"prior\" or \"all\". If passing \"prior\", the full message will be required too. Including all passed flags")
let ncSound = argParser.add(option: "--sound", kind: String.self, usage: "sound to play. Pass \"default\" for the default macOS sound, else the name of a sound in /Library/Sounds or /System/Library/Sounds. If the sound cannot be found, macOS will use the \"default\" sound" )
let ncSubtitle = argParser.add(option: "--subtitle", kind: String.self, usage: "message subtitle")
let ncTitle = argParser.add(option: "--title", kind: String.self, usage: "message title")
let ncType = argParser.add(option: "--type", kind: String.self, usage: "alert or banner - REQUIRED")
let ncVerbose = argParser.add(option: "--verbose", kind: Bool.self, usage: "Enables logging of actions. Check console for 'Notifier Log:' messages")

// The first argument is always the executable, drop it
let passedArgs = Array(CommandLine.arguments.dropFirst())

Expand All @@ -54,30 +42,37 @@ class AppDelegate: NSObject, NSApplicationDelegate {

// Get the parsed args
let parsedResult = try argParser.parse(passedArgs)

// If verbose mode is enabled
verboseMode = parsedResult.get(ncVerbose) ?? false
verboseMode = parsedResult.verbose ?? false
if verboseMode {
NSLog("Notifier Log: notifier - verbose enabled")
notifierArgsArray.append("--verbose")
notifierArgsArray.append(String(verboseMode))
}

// Check parsed args to make sure at least base args are found, if not show help
if (parsedResult.get(ncType) != nil){
if ((parsedResult.get(ncMessage) == "") || (parsedResult.get(ncMessage) == nil )) && ((parsedResult.get(ncRemove) == "") || (parsedResult.get(ncRemove) == nil)){
if parsedResult.type != nil {
if ((parsedResult.message == "") || (parsedResult.message == nil )) && ((parsedResult.remove == "") || (parsedResult.remove == nil)){
try _ = argParser.parse(["--help"])
} else {
parsedType = parsedResult.get(ncType)!.lowercased()
if parsedType != "alert" && parsedType != "banner" {
if let type = parsedResult.type {
if type != "alert" && type != "banner" {
try _ = argParser.parse(["--help"])
} else {
parsedType = type.lowercased()
}
} else {
try _ = argParser.parse(["--help"])
}
}
} else {
try _ = argParser.parse(["--help"])
}


// If --remove all passed
if (parsedResult.get(ncRemove) == "all") {
if (parsedResult.remove == "all") {
if verboseMode {
NSLog("Notifier Log: notifier - remove all")
}
Expand All @@ -87,85 +82,113 @@ class AppDelegate: NSObject, NSApplicationDelegate {
// If not parse other args
} else {

if (parsedResult.get(ncRemove) == "prior") {
if (parsedResult.remove == "prior") {
if verboseMode {
NSLog("Notifier Log: notifier - remove prior")
}
notifierArgsArray.append("--remove")
notifierArgsArray.append("prior")
}

if (parsedResult.get(ncMessage) != nil) {
if (parsedResult.message != nil) {
if verboseMode {
NSLog("Notifier Log: notifier - message")
}
notifierArgsArray.append("--message")
if (parsedResult.get(ncMessage) == "") {
if (parsedResult.message == "") {
notifierArgsArray.append(" ")
} else {
notifierArgsArray.append(parsedResult.get(ncMessage)!)
if let message = parsedResult.message {
notifierArgsArray.append(message)
} else {
try _ = argParser.parse(["--help"])
}
}
if verboseMode {
NSLog("Notifier Log: notifier - notifierArgsArray - %@", notifierArgsArray)
}
}
if (parsedResult.get(ncMessageAction) != nil) {

if (parsedResult.messageaction != nil) {
if verboseMode {
NSLog("Notifier Log: notifier - messageaction")
}
notifierArgsArray.append("--messageaction")
notifierArgsArray.append(parsedResult.get(ncMessageAction)!)
if let messageaction = parsedResult.messageaction {
notifierArgsArray.append(messageaction)
} else {
try _ = argParser.parse(["--help"])
}
if verboseMode {
NSLog("Notifier Log: notifier - notifierArgsArray - %@", notifierArgsArray)
}
}

if (parsedResult.get(ncSound) != nil) {
if (parsedResult.sound != nil) {
if verboseMode {
NSLog("Notifier Log: notifier - sound")
}
notifierArgsArray.append("--sound")
notifierArgsArray.append(parsedResult.get(ncSound)!)
if let sound = parsedResult.sound {
notifierArgsArray.append(sound)
} else {
try _ = argParser.parse(["--help"])
}
if verboseMode {
NSLog("Notifier Log: notifier - notifierArgsArray - %@", notifierArgsArray)
}
}

if (parsedResult.get(ncSubtitle) != nil) {
if (parsedResult.subtitle != nil) {
if verboseMode {
NSLog("Notifier Log: notifier - subtitle")
}
notifierArgsArray.append("--subtitle")
notifierArgsArray.append(parsedResult.get(ncSubtitle)!)
if let subtitle = parsedResult.subtitle {
notifierArgsArray.append(subtitle)
} else {
try _ = argParser.parse(["--help"])
}
if verboseMode {
NSLog("Notifier Log: notifier - notifierArgsArray - %@", notifierArgsArray)
}
}

if (parsedResult.get(ncTitle) != nil) {
if (parsedResult.title != nil) {
if verboseMode {
NSLog("Notifier Log: notifier - title")
}
notifierArgsArray.append("--title")
notifierArgsArray.append(parsedResult.get(ncTitle)!)
if let title = parsedResult.title {
notifierArgsArray.append(title)
} else {
try _ = argParser.parse(["--help"])
}
if verboseMode {
NSLog("Notifier Log: notifier - notifierArgsArray - %@", notifierArgsArray)
}
}

if parsedType == "alert" && (parsedResult.get(ncMessageButton) != nil) {
if parsedType == "alert" && (parsedResult.messagebutton != nil) {
if verboseMode {
NSLog("Notifier Log: notifier - messagebutton")
}
notifierArgsArray.append("--messagebutton")
notifierArgsArray.append(parsedResult.get(ncMessageButton)!)
if (parsedResult.get(ncMessageButtonAction) != nil){
if let messagebutton = parsedResult.messagebutton {
notifierArgsArray.append(messagebutton)
} else {
try _ = argParser.parse(["--help"])
}
if (parsedResult.messagebuttonaction != nil){
if verboseMode {
NSLog("Notifier Log: notifier - messagebuttonaction")
}
notifierArgsArray.append("--messagebuttonaction")
notifierArgsArray.append(parsedResult.get(ncMessageButtonAction)!)
if let messagebuttonaction = parsedResult.messagebuttonaction {
notifierArgsArray.append(messagebuttonaction)
} else {
try _ = argParser.parse(["--help"])
}
}
if verboseMode {
NSLog("Notifier Log: notifier - notifierArgsArray - %@", notifierArgsArray)
Expand All @@ -174,24 +197,25 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}

// If we're missing a value for an arg
} catch ArgumentParserError.expectedValue(let value) {
print("Missing value for argument \(value).")
if verboseMode {
NSLog("Notifier Log: notifier - Missing value for argument \(value).")
}
exit(1)
// } catch ArgumentParserError.expectedValue(let value) {
// print("Missing value for argument \(value).")
// if verboseMode {
// NSLog("Notifier Log: notifier - Missing value for argument \(value).")
// }
// exit(1)
// If we're missing a value for an arg
} catch ArgumentParserError.expectedArguments( _, let stringArray) {
print("Missing arguments: \(stringArray.joined()).")
if verboseMode {
NSLog("Notifier Log: notifier - Missing value for argument \(stringArray.joined()).")
}
exit(1)
// } catch ArgumentParserError.expectedArguments( _, let stringArray) {
// print("Missing arguments: \(stringArray.joined()).")
// if verboseMode {
// NSLog("Notifier Log: notifier - Missing value for argument \(stringArray.joined()).")
// }
// exit(1)
// Other errors
} catch {
print(error.localizedDescription)
let message = argParser.message(for: error)
print(message)
if verboseMode {
NSLog("Notifier Log: notifier - \(error.localizedDescription).")
NSLog("Notifier Log: notifier - \(message).")
}
exit(1)
}
Expand Down
Loading

0 comments on commit 1c235c2

Please sign in to comment.