dry up the help logic

clip/screenshot are now command groups.
This commit is contained in:
onyx-and-iris 2025-07-01 09:08:15 +01:00
parent 8f0bde800c
commit 74df0e7022
3 changed files with 108 additions and 80 deletions

114
index.js
View File

@ -8,8 +8,8 @@ import { sceneHelp, sceneList, sceneSwitch, sceneCurrent } from './utils/scene.j
import { audioHelp, audioList, audioMute, audioUnmute, audioToggle, audioStatus } from './utils/audio.js' import { audioHelp, audioList, audioMute, audioUnmute, audioToggle, audioStatus } from './utils/audio.js'
import { streamHelp, streamStart, streamStop, streamToggle, streamStatus } from './utils/stream.js' import { streamHelp, streamStart, streamStop, streamToggle, streamStatus } from './utils/stream.js'
import { recordHelp, recordStart, recordStop, recordToggle, recordStatus } from './utils/record.js' import { recordHelp, recordStart, recordStop, recordToggle, recordStatus } from './utils/record.js'
import { recordClip } from './utils/clip.js' import { clipHelp, saveClip } from './utils/clip.js'
import { recordScreenshot } from './utils/screenshot.js' import { screenshotHelp, takeScreenshot } from './utils/screenshot.js'
const input = cli.input const input = cli.input
const flags = cli.flags const flags = cli.flags
@ -19,6 +19,15 @@ const port = flags.port ?? process.env.MELD_CLI_PORT ?? 13376
const socket = new WebSocket(`ws://${address}:${port}`) const socket = new WebSocket(`ws://${address}:${port}`)
/**
* Print help information.
* @param {string} helpText
*/
function printHelp(helpText) {
console.log(helpText)
process.exit(0)
}
/** /**
* Helper to wrap QWebChannel usage and handle promise-based command execution. * Helper to wrap QWebChannel usage and handle promise-based command execution.
* @param {WebSocket} socket - The websocket instance. * @param {WebSocket} socket - The websocket instance.
@ -49,13 +58,21 @@ function withChannel(socket, fn) {
socket.onopen = function () { socket.onopen = function () {
(() => { (() => {
try { try {
if (input[0] === 'scene') { const command = input[0]
if (flags.help) { const helpMap = {
console.log(sceneHelp) scene: sceneHelp,
socket.close() audio: audioHelp,
process.exit(0) stream: streamHelp,
} record: recordHelp,
clip: clipHelp,
screenshot: screenshotHelp
}
if (flags.help && helpMap[command]) {
printHelp(helpMap[command])
}
if (command === 'scene') {
const [sceneCommand, ...sceneArguments] = input.slice(1) const [sceneCommand, ...sceneArguments] = input.slice(1)
switch (sceneCommand) { switch (sceneCommand) {
case 'list': case 'list':
@ -72,19 +89,10 @@ socket.onopen = function () {
withChannel(socket, (channel) => sceneCurrent(channel, flags.id)) withChannel(socket, (channel) => sceneCurrent(channel, flags.id))
break break
default: default:
console.log(sceneHelp) printHelp(sceneHelp)
socket.close()
process.exit(0)
} }
} else if (input[0] === 'audio') { } else if (command === 'audio') {
if (flags.help) { const [audioCommand, ...audioArguments] = input.slice(1)
console.log(audioHelp)
socket.close()
process.exit(0)
}
const audioCommand = input[1]
const audioName = input[2]
switch (audioCommand) { switch (audioCommand) {
case 'list': case 'list':
withChannel(socket, (channel) => audioList(channel, flags.id)) withChannel(socket, (channel) => audioList(channel, flags.id))
@ -94,33 +102,25 @@ socket.onopen = function () {
console.error('Error: Audio name is required for the mute command.') console.error('Error: Audio name is required for the mute command.')
process.exit(1) process.exit(1)
} }
withChannel(socket, (channel) => audioMute(channel, audioName)) withChannel(socket, (channel) => audioMute(channel, audioArguments[0]))
break break
case 'unmute': case 'unmute':
if (!audioName) { if (!audioArguments[0]) {
console.error('Error: Audio name is required for the unmute command.') console.error('Error: Audio name is required for the unmute command.')
process.exit(1) process.exit(1)
} }
withChannel(socket, (channel) => audioUnmute(channel, audioName)) withChannel(socket, (channel) => audioUnmute(channel, audioArguments[0]))
break break
case 'toggle': case 'toggle':
withChannel(socket, (channel) => audioToggle(channel, audioName)) withChannel(socket, (channel) => audioToggle(channel, audioArguments[0]))
break break
case 'status': case 'status':
withChannel(socket, (channel) => audioStatus(channel, audioName)) withChannel(socket, (channel) => audioStatus(channel, audioArguments[0]))
break break
default: default:
console.log(audioHelp) printHelp(audioHelp)
socket.close()
process.exit(0)
} }
} else if (input[0] === 'stream') { } else if (command === 'stream') {
if (flags.help) {
console.log(streamHelp)
socket.close()
process.exit(0)
}
const streamCommand = input[1] const streamCommand = input[1]
switch (streamCommand) { switch (streamCommand) {
case 'start': case 'start':
@ -140,13 +140,7 @@ socket.onopen = function () {
socket.close() socket.close()
process.exit(0) process.exit(0)
} }
} else if (input[0] === 'record') { } else if (command === 'record') {
if (flags.help) {
console.log(recordHelp)
socket.close()
process.exit(0)
}
const recordCommand = input[1] const recordCommand = input[1]
switch (recordCommand) { switch (recordCommand) {
case 'start': case 'start':
@ -162,33 +156,29 @@ socket.onopen = function () {
withChannel(socket, (channel) => recordStatus(channel)) withChannel(socket, (channel) => recordStatus(channel))
break break
default: default:
console.log(recordHelp) printHelp(recordHelp)
socket.close()
process.exit(0)
} }
} else if (input[0] === 'clip') { } else if (command === 'clip') {
if (flags.help) { const clipCommand = input[1]
console.log(`usage: meld-cli clip`) if (clipCommand === 'save') {
socket.close() withChannel(socket, (channel) => saveClip(channel))
process.exit(0) } else {
printHelp(clipHelp)
} }
} else if (command === 'screenshot') {
withChannel(socket, (channel) => recordClip(channel)) const screenshotCommand = input[1]
} else if (input[0] === 'screenshot') { if (screenshotCommand === 'take') {
if (flags.help) { withChannel(socket, (channel) => takeScreenshot(channel))
console.log(`usage: meld-cli screenshot`) } else {
socket.close() printHelp(screenshotHelp)
process.exit(0)
} }
withChannel(socket, (channel) => recordScreenshot(channel))
} else { } else {
console.log('Unknown command. Use meld-cli --help for available commands.') printHelp(cli.help)
socket.close() socket.close()
process.exit(0) process.exit(1)
} }
} catch (error) { } catch (error) {
console.error('Error handling CLI flags:', error) console.error(`Error: ${error.message}`)
} }
})() })()
} }

View File

@ -1,11 +1,30 @@
function recordClip(channel) { import meowHelp from 'cli-meow-help'
return new Promise((resolve, reject) => {
channel.objects.meld.sendCommand('meld.recordClip').then(() => { const clipHelp = meowHelp({
resolve('Clip command sent successfully.') name: 'meld-cli clip',
}).catch((err) => { desc: 'Manage clips in Meld',
reject(err) commands: {
}) save: {
}) desc: 'Save a clip'
}
},
flags: {
help: {
type: 'boolean',
shortFlag: 'h',
desc: 'Display help information'
}
},
defaults: false
})
async function saveClip(channel) {
try {
await channel.objects.meld.sendCommand('meld.recordClip');
return 'Clip command sent successfully.';
} catch (err) {
throw err;
}
} }
export { recordClip } export { clipHelp, saveClip }

View File

@ -1,11 +1,30 @@
function recordScreenshot(channel) { import meowHelp from 'cli-meow-help'
return new Promise((resolve, reject) => {
channel.objects.meld.sendCommand('meld.screenshot').then(() => { const screenshotHelp = meowHelp({
resolve('Screenshot command sent successfully.') name: 'meld-cli screenshot',
}).catch((err) => { desc: 'Manage screenshots in Meld',
reject(err) commands: {
}) take: {
}) desc: 'Take a screenshot'
}
},
flags: {
help: {
type: 'boolean',
shortFlag: 'h',
desc: 'Display help information'
}
},
defaults: false
})
async function takeScreenshot(channel) {
try {
await channel.objects.meld.sendCommand('meld.screenshot');
return 'Screenshot command sent successfully.';
} catch (err) {
throw err;
}
} }
export { recordScreenshot } export { screenshotHelp, takeScreenshot }