kareman / moderator Goto Github PK
View Code? Open in Web Editor NEWA simple, modular command line argument parser in Swift.
License: MIT License
A simple, modular command line argument parser in Swift.
License: MIT License
In Swift 3 version of SwiftShell this code was working perfectly:
`let unavailState: String = run("/bin/bash", with: [ "-c", "/usr/local/bin/zpool status | grep state | grep -w UNAVAIL | wc -l"])
let trimUnavail = unavailState.trimmingCharacters(in: .whitespacesAndNewlines)
let degradedState: String = run("/bin/bash", with: [ "-c", "/usr/local/bin/zpool status | grep state | grep -w DEGRADED | wc -l"])
let trimDegraded = degradedState.trimmingCharacters(in: .whitespacesAndNewlines)
let offlineState: String = run("/bin/bash", with: ["-c", "/usr/local/bin/zpool status | grep state | grep -w OFFLINE | wc -l"])
let trimOffline = offlineState.trimmingCharacters(in: .whitespacesAndNewlines)
let faultedState: String = run("/bin/bash", with: ["-c", "/usr/local/bin/zpool status | grep state | grep -w FAULTED | wc -l"])
let trimFaulted = faultedState.trimmingCharacters(in: .whitespacesAndNewlines)
let removedState: String = run("/bin/bash", with: ["-c", "/usr/local/bin/zpool status | grep state | grep -w REMOVED | wc -l"])
let trimRemoved = removedState.trimmingCharacters(in: .whitespacesAndNewlines)
let onlineState: String = run("/bin/bash", with: ["-c", "/usr/local/bin/zpool status | grep state | grep -w ONLINE | wc -l"])
let trimOnline = onlineState.trimmingCharacters(in: .whitespacesAndNewlines)
But now, it throw an error while compiling:
Argument labels '(_:, with:)' do not match any available overloads`
So, what is new way to do the same thing?
I’m using Swift on a Raspberry Pi for aquarium automation. The issue is that we are stuck with Swift 3.1.1 for the time being, but still want to leverage libraries like this one. And this one was the easiest to bootstrap back into Swift 3 for my purposes.
I’ve put together a set of changes in my fork that add extensions to String for the Swift 4-only APIs you call, based on the original Swift 3 code you had. I also changed the Travis yml file so that CI builds check both 3.1.1 and 4.1.2. While I wouldn’t expect folks to test against both versions of Swift, and they could accidentally use a Swift 4 API without thinking of compatibility, the CI should help catch those sorts of issues.
The one change I had to make that I’m not entirely happy with has to do with the empty tuple you use during a map to save space. The code doesn’t care about the result, so it makes sense, but it only really works in Swift 4. Swift 3 complains about using an empty tuple in this way. I made it work by using a Bool instead, but perhaps there’s a better approach here that I’m missing.
I can send the Pull Request along as it has been working well for my project, and Travis is happy, except that flatMap apparently just got deprecated in Swift 4.1 (just a warning for now). Go figure.
Is there a way to create an argument that allows spaces in it?
I am trying to use Moderator for a swift script to post a message to the Slack API. How can i pass in a "message" argument that may have spaces in it? Wrapping my message argument value in quotes does not seem to work.
I imagine I could write my messages to a file, and pass in the filename, but I was curious if there is any other way with the current Moderator implementation.
import Foundation
import Moderator
import SwiftShell
let arguments = Moderator(description: "Post a message to Slack.")
let messageArg = arguments.add(Argument<String?>.optionWithValue("m", name: "message", description: "The message to post."))
let botNameArg = arguments.add(Argument<String?>.optionWithValue("n", name: "name", description: "The name of the bot."))
let urlSlugArg = arguments.add(Argument<String>.optionWithValue("p", name: "path", description: "The url path (the part after https://hooks.slack.com/services/)."))
do {
try arguments.parse()
guard let message = messageArg.value else {
throw ArgumentError(errormessage: "Slack message not found.", usagetext: "use -m Message")
}
guard let urlSlug = urlSlugArg.value else {
throw ArgumentError(errormessage: "Slack URL path not found.", usagetext: "use -p T000000/B000000/XXXXXXX")
}
guard let botName = botNameArg.value else {
throw ArgumentError(errormessage: "Bot name not found.", usagetext: "use -n BotName")
}
} catch {
print("*** FAILURE!")
print(error)
exit(Int32(error._code))
}
Here is what happens with my current script. I am trying to get the "m" argument to contain spaces:
$ marathon run post-to-slack.swift -m Message! -p T00000000/B00000000/XXXXXXXXXXXX -n Bot
$ marathon run post-to-slack.swift -m Longer Message! -p T0000000/B0000000/XXXXXXXXXXXX -n Bot
*** FAILURE!
Unknown arguments: Message!
Post a message to Slack.
Usage: post-to-slack
-m <message>:
The message to post.
-n <name>:
The name of the bot.
-p <path>:
The url path (the part after https://hooks.slack.com/services/).
$ marathon run post-to-slack.swift -m "Longer Message!" -p T00000000/B00000000/XXXXXXXXXXXXXX -n Bot
-bash: !": event not found
Thanks you for any guidance you can help provide. Loving your tools.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.