add command-line option parsing to python
This commit is contained in:
parent
759682ef8a
commit
dda412c85f
2
TODO
2
TODO
|
@ -1,4 +1,4 @@
|
|||
- writing binding.xml
|
||||
- python should have some way to do cmd-line args
|
||||
|
||||
- test other cpp arg types
|
||||
|
||||
|
|
|
@ -62,6 +62,33 @@ from gi.repository import Vips
|
|||
The C++ API takes this route.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
You can generate searchable docs from a <code>.gir</code> (the thing that
|
||||
is built from scanning libvips and which in turn turn the typelib is
|
||||
made from) with <command>g-ir-doc-tool</command>, for example:
|
||||
|
||||
<programlisting language="bash">
|
||||
$ g-ir-doc-tool --language=Python -o ~/mydocs Vips-8.0.gir
|
||||
</programlisting>
|
||||
|
||||
Then to view them, either:
|
||||
|
||||
<programlisting language="bash">
|
||||
$ yelp ~/mydocs
|
||||
</programlisting>
|
||||
|
||||
Or perhaps
|
||||
|
||||
<programlisting language="bash">
|
||||
$ cd ~/mydocs
|
||||
$ yelp-build html .
|
||||
</programlisting>
|
||||
|
||||
To make HTML docs. This is an easy way to see what you can call in the
|
||||
library.
|
||||
</para>
|
||||
|
||||
|
||||
</refsect1>
|
||||
|
||||
</refentry>
|
||||
|
|
|
@ -363,4 +363,26 @@ result_image = image.sin()
|
|||
</para>
|
||||
</refsect1>
|
||||
|
||||
<refsect1 id="python-args">
|
||||
<title>Command-line option parsing</title>
|
||||
<para>
|
||||
GLib includes a command-line option parser, and Vips defines a set of
|
||||
standard flags you can use with it. For example:
|
||||
|
||||
<programlisting language="Python">
|
||||
import sys
|
||||
from gi.repository import GLib, Vips
|
||||
|
||||
context = GLib.OptionContext(" - test stuff")
|
||||
main_group = GLib.OptionGroup("main",
|
||||
"Main options", "Main options for this program",
|
||||
None)
|
||||
context.set_main_group(main_group)
|
||||
Vips.add_option_entries(main_group)
|
||||
context.parse(sys.argv)
|
||||
</programlisting>
|
||||
</para>
|
||||
|
||||
</refsect1>
|
||||
|
||||
</refentry>
|
||||
|
|
|
@ -5402,3 +5402,18 @@ vips_check_dmask_1d( const char *domain, DOUBLEMASK *mask )
|
|||
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
GOptionGroup *
|
||||
vips_get_option_group( void )
|
||||
{
|
||||
static GOptionGroup *option_group = NULL;
|
||||
|
||||
if( !option_group ) {
|
||||
option_group = g_option_group_new( "vips",
|
||||
_( "VIPS Options" ), _( "Show VIPS options" ),
|
||||
NULL, NULL );
|
||||
vips_add_option_entries( option_group );
|
||||
}
|
||||
|
||||
return( option_group );
|
||||
}
|
||||
|
|
|
@ -183,7 +183,7 @@ const char *vips_get_argv0( void );
|
|||
void vips_shutdown( void );
|
||||
void vips_thread_shutdown( void );
|
||||
|
||||
GOptionGroup *vips_get_option_group( void );
|
||||
void vips_add_option_entries( GOptionGroup *option_group );
|
||||
|
||||
extern void vips_leak_set( gboolean leak );
|
||||
|
||||
|
|
|
@ -1234,6 +1234,8 @@ int vips_check_imask( const char *domain, INTMASK *mask );
|
|||
int vips_check_dmask( const char *domain, DOUBLEMASK *mask );
|
||||
int vips_check_dmask_1d( const char *domain, DOUBLEMASK *mask );
|
||||
|
||||
GOptionGroup *vips_get_option_group( void );
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif /*__cplusplus*/
|
||||
|
|
|
@ -630,30 +630,17 @@ static GOptionEntry option_entries[] = {
|
|||
};
|
||||
|
||||
/**
|
||||
* vips_get_option_group: (skip)
|
||||
* vips_add_option_entries:
|
||||
* @option_group: group to add to
|
||||
*
|
||||
* vips_get_option_group() returns a %GOptionGroup containing various VIPS
|
||||
* command-line options. It can be used with %GOption to help
|
||||
* parse argc/argv.
|
||||
* Add the standard vips %GOptionEntry to a %GOptionGroup.
|
||||
*
|
||||
* See also: vips_version(), vips_guess_prefix(),
|
||||
* vips_guess_libdir(), vips_init().
|
||||
*
|
||||
* Returns: a %GOptionGroup for VIPS, see %GOption
|
||||
* See also: g_option_group_new().
|
||||
*/
|
||||
GOptionGroup *
|
||||
vips_get_option_group( void )
|
||||
void
|
||||
vips_add_option_entries( GOptionGroup *option_group )
|
||||
{
|
||||
static GOptionGroup *option_group = NULL;
|
||||
|
||||
if( !option_group ) {
|
||||
option_group = g_option_group_new(
|
||||
"vips", _( "VIPS Options" ), _( "Show VIPS options" ),
|
||||
NULL, NULL );
|
||||
g_option_group_add_entries( option_group, option_entries );
|
||||
}
|
||||
|
||||
return( option_group );
|
||||
g_option_group_add_entries( option_group, option_entries );
|
||||
}
|
||||
|
||||
/* Find the prefix part of a dir ... name is the name of this prog from argv0.
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
#!/usr/bin/python
|
||||
|
||||
import sys
|
||||
|
||||
from gi.repository import GLib, Vips
|
||||
|
||||
context = GLib.OptionContext(" - test python stuff")
|
||||
main_group = GLib.OptionGroup("main",
|
||||
"Main options", "Main options for this program",
|
||||
None)
|
||||
context.set_main_group(main_group)
|
||||
Vips.add_option_entries(main_group)
|
||||
context.parse(sys.argv)
|
||||
|
||||
|
|
@ -1042,8 +1042,8 @@ main( int argc, char **argv )
|
|||
|
||||
GError *error = NULL;
|
||||
|
||||
if( im_init_world( argv[0] ) )
|
||||
error_exit( NULL );
|
||||
if( VIPS_INIT( argv[0] ) )
|
||||
vips_error_exit( NULL );
|
||||
textdomain( GETTEXT_PACKAGE );
|
||||
setlocale( LC_ALL, "" );
|
||||
|
||||
|
@ -1068,13 +1068,10 @@ main( int argc, char **argv )
|
|||
*/
|
||||
main_group = g_option_group_new( NULL, NULL, NULL, NULL, NULL );
|
||||
g_option_group_add_entries( main_group, main_option );
|
||||
vips_add_option_entries( main_group );
|
||||
g_option_group_set_translation_domain( main_group, GETTEXT_PACKAGE );
|
||||
g_option_context_set_main_group( context, main_group );
|
||||
|
||||
/* Add the libvips options too.
|
||||
*/
|
||||
g_option_context_add_group( context, im_get_option_group() );
|
||||
|
||||
/* We add more options later, for example as options to vips8
|
||||
* operations. Ignore any unknown options in this first parse.
|
||||
*/
|
||||
|
|
|
@ -126,19 +126,24 @@ int
|
|||
main( int argc, char **argv )
|
||||
{
|
||||
GOptionContext *context;
|
||||
GOptionGroup *main_group;
|
||||
GError *error = NULL;
|
||||
IMAGE *im;
|
||||
unsigned char header[IM_SIZEOF_HEADER];
|
||||
|
||||
if( im_init_world( argv[0] ) )
|
||||
error_exit( "%s", _( "unable to start VIPS" ) );
|
||||
if( VIPS_INIT( argv[0] ) )
|
||||
vips_error_exit( "%s", _( "unable to start VIPS" ) );
|
||||
textdomain( GETTEXT_PACKAGE );
|
||||
setlocale( LC_ALL, "" );
|
||||
|
||||
context = g_option_context_new(
|
||||
_( "vipsedit - edit vips file header" ) );
|
||||
g_option_context_add_main_entries( context, entries, GETTEXT_PACKAGE );
|
||||
g_option_context_add_group( context, im_get_option_group() );
|
||||
main_group = g_option_group_new( NULL, NULL, NULL, NULL, NULL );
|
||||
g_option_group_add_entries( main_group, entries );
|
||||
vips_add_option_entries( main_group );
|
||||
g_option_group_set_translation_domain( main_group, GETTEXT_PACKAGE );
|
||||
g_option_context_set_main_group( context, main_group );
|
||||
|
||||
if( !g_option_context_parse( context, &argc, &argv, &error ) ) {
|
||||
if( error ) {
|
||||
fprintf( stderr, "%s\n", error->message );
|
||||
|
|
|
@ -179,20 +179,22 @@ int
|
|||
main( int argc, char *argv[] )
|
||||
{
|
||||
GOptionContext *context;
|
||||
GOptionGroup *main_group;
|
||||
GError *error = NULL;
|
||||
int i;
|
||||
int result;
|
||||
|
||||
if( vips__init( argv[0] ) )
|
||||
if( VIPS_INIT( argv[0] ) )
|
||||
vips_error_exit( "unable to start VIPS" );
|
||||
textdomain( GETTEXT_PACKAGE );
|
||||
setlocale( LC_ALL, "" );
|
||||
|
||||
context = g_option_context_new( _( "- print image header" ) );
|
||||
|
||||
g_option_context_add_main_entries( context,
|
||||
main_option, GETTEXT_PACKAGE );
|
||||
g_option_context_add_group( context, vips_get_option_group() );
|
||||
main_group = g_option_group_new( NULL, NULL, NULL, NULL, NULL );
|
||||
g_option_group_add_entries( main_group, main_option );
|
||||
vips_add_option_entries( main_group );
|
||||
g_option_group_set_translation_domain( main_group, GETTEXT_PACKAGE );
|
||||
g_option_context_set_main_group( context, main_group );
|
||||
|
||||
if( !g_option_context_parse( context, &argc, &argv, &error ) ) {
|
||||
if( error ) {
|
||||
|
|
|
@ -760,10 +760,11 @@ int
|
|||
main( int argc, char **argv )
|
||||
{
|
||||
GOptionContext *context;
|
||||
GOptionGroup *main_group;
|
||||
GError *error = NULL;
|
||||
int i;
|
||||
|
||||
if( vips__init( argv[0] ) )
|
||||
if( VIPS_INIT( argv[0] ) )
|
||||
vips_error_exit( "unable to start VIPS" );
|
||||
textdomain( GETTEXT_PACKAGE );
|
||||
setlocale( LC_ALL, "" );
|
||||
|
@ -776,8 +777,11 @@ main( int argc, char **argv )
|
|||
|
||||
context = g_option_context_new( _( "- thumbnail generator" ) );
|
||||
|
||||
g_option_context_add_main_entries( context, options, GETTEXT_PACKAGE );
|
||||
g_option_context_add_group( context, vips_get_option_group() );
|
||||
main_group = g_option_group_new( NULL, NULL, NULL, NULL, NULL );
|
||||
g_option_group_add_entries( main_group, options );
|
||||
vips_add_option_entries( main_group );
|
||||
g_option_group_set_translation_domain( main_group, GETTEXT_PACKAGE );
|
||||
g_option_context_set_main_group( context, main_group );
|
||||
|
||||
if( !g_option_context_parse( context, &argc, &argv, &error ) ) {
|
||||
if( error ) {
|
||||
|
|
Loading…
Reference in New Issue